Skip to content

Commit

Permalink
Merge pull request #21 from samthakur587/ast
Browse files Browse the repository at this point in the history
printing ast removed
  • Loading branch information
samthakur587 authored Jul 31, 2024
2 parents c5664f7 + a3aca47 commit 4d434ee
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 174 deletions.
63 changes: 61 additions & 2 deletions getting_start.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -493,6 +493,65 @@
"print(glsl_code)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from src.backend.Opengl import *"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"shader main {\n",
" vertex {\n",
" input vec3 position;\n",
" input vec2 texCoord;\n",
" input vec2 vTexCoord;\n",
" output vec2 vTexCoord;\n",
"\n",
" output vec4 fragColor;\n",
"\n",
"\n",
"\n",
" void main() {\n",
" vTexCoord = texCoord;\n",
" gl_Position = vec4(position, 1.0);\n",
" }\n",
" }\n",
" fragment {\n",
"\n",
"\n",
" float marblePattern(vec2 uv) {\n",
" return (0.5 + (0.5 * sin(((10.0 * uv.x) + iTime))));\n",
" }\n",
" void main() {\n",
" color = vec3(marblePattern(vTexCoord), 0.0, 0.0);\n",
" fragColor = vec4(color, 1.0);\n",
" }\n",
" }\n",
"}\n",
"\n"
]
}
],
"source": [
"lexer = Lexer(glsl_code)\n",
"parser = Parser(lexer.tokens)\n",
"ast = parser.parse()\n",
"codegen = CrossglCodeGen()\n",
"cross_code = codegen.generate(ast)\n",
"print(cross_code)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -629,7 +688,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand Down
4 changes: 2 additions & 2 deletions src/backend/DirectX/DirectxCrossGLCodeGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ def generate_expression(self, expr, is_main=False):
right = self.generate_expression(expr.right, is_main)
return f"({left} {expr.op} {right})"
elif isinstance(expr, UnaryOpNode):
operand = self.generate_expression(expr.operand, is_main)
return f"({expr.operator}{operand})"
operand = self.generate_expression(expr.op, is_main)
return f"({expr.op}{operand})"
elif isinstance(expr, FunctionCallNode):
args = ", ".join(
self.generate_expression(arg, is_main) for arg in expr.args
Expand Down
32 changes: 0 additions & 32 deletions src/backend/Opengl/OpenglLexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,35 +116,3 @@ def tokenize(self):
)

self.tokens.append(("EOF", None)) # End of file token


if __name__ == "__main__":
code = """
#version 330 core
// Vertex Shader
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoord;
out vec2 fragTexCoord;
void main() {
gl_Position = vec4(position, 1.0);
fragTexCoord = texCoord;
}
// Fragment Shader
in vec2 fragTexCoord;
out vec4 color;
uniform sampler2D textureSampler;
void main() {
color = texture(textureSampler, fragTexCoord);
}
"""
lexer = Lexer(code)
for token in lexer.tokens:
print(token)
44 changes: 0 additions & 44 deletions src/backend/Opengl/OpenglParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,13 @@ def parse_shader(self, version_node):
comment_content = (
self.current_token[1].strip().lower()
) # Normalize content
print(f"Comment content: '{comment_content}'")

if "vertex shader" in comment_content:
current_section = "VERTEX"
print("Switched to VERTEX section")
elif "fragment shader" in comment_content:
current_section = "FRAGMENT"
print("Switched to FRAGMENT section")
else:
current_section = "VERTEX"
print("Defaulting to VERTEX section")

self.eat("COMMENT_SINGLE")

Expand Down Expand Up @@ -649,43 +645,3 @@ def parse_member_access(self, object):
return self.parse_member_access(MemberAccessNode(object, member))

return MemberAccessNode(object, member)


if __name__ == "__main__":
code = """
#version 330 core
// Vertex Shader
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoord;
out vec2 fragTexCoord;
void main() {
gl_Position = vec4(position, 1.0);
fragTexCoord = texCoord;
}
// Fragment Shader
in vec2 fragTexCoord;
out vec4 color;
uniform sampler2D textureSampler;
void main() {
color = texture(textureSampler, fragTexCoord);
}
"""
lexer = Lexer(code)
parser = Parser(lexer.tokens)
for token in lexer.tokens:
print(token)
parser = Parser(lexer.tokens)
ast = parser.parse()

print("Parsing completed successfully!")
# print(ast)
# print("Parsed AST:")
# print(parser.parse())
46 changes: 1 addition & 45 deletions src/backend/Opengl/openglCrossglCodegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def generate_shader(self, node):
if self.vertex_item:
code += " vertex {\n"
# Print the vertex section to check its content
print(f"Vertex section: {self.vertex_item}")
code += self.generate_layouts(self.vertex_item.layout_qualifiers)
# Process inputs and outputs
for vtype, name in self.shader_inputs:
Expand Down Expand Up @@ -98,9 +97,6 @@ def generate_functions(self, functions, shader_type):

if shader_type in ["vertex", "fragment"]:
for function_node in functions:
print(
f"Function: {function_node.name}, Return Type: {function_node.return_type}, Params: {function_node.params}"
)
# Generate parameter list
params = ", ".join(
f"{self.map_type(param[0])} {param[1]}"
Expand Down Expand Up @@ -178,6 +174,7 @@ def generate_expression(self, expr):
args = ", ".join(self.generate_expression(arg) for arg in expr.args)
func_name = self.translate_expression(expr.name)
return f"{func_name}({args})"

elif isinstance(expr, MemberAccessNode):
return f"{self.generate_expression(expr.object)}.{expr.member}"
else:
Expand Down Expand Up @@ -213,44 +210,3 @@ def map_operator(self, op):
}
return op_map.get(op, op)


if __name__ == "__main__":
code = """
#version 450
// Vertex Shader
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec2 inTexCoord;
out vec2 fragTexCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(inPosition, 1.0);
fragTexCoord = inTexCoord;
}
// Fragment Shader
in vec2 fragTexCoord;
out vec4 color;
uniform sampler2D textureSampler;
void main() {
color = texture(textureSampler, fragTexCoord);
}
"""

lexer = Lexer(code)
parser = Parser(lexer.tokens)
ast = parser.parse()
print("Parsing completed successfully!")
# print(ast)
codegen = CrossglCodeGen()
cross_code = codegen.generate(ast)
print(cross_code)
11 changes: 9 additions & 2 deletions src/translator/codegen/directx_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
MemberAccessNode,
VERTEXShaderNode,
FRAGMENTShaderNode,
TernaryOpNode,
)


Expand Down Expand Up @@ -144,7 +145,7 @@ def generate_function(self, node, shader_type):
)
return_type = self.map_type(function_node.return_type)

code += f"{return_type} {function_node.name}({params}) {{\n"
code += f"{return_type} VSMain({params}) {{\n"
if function_node.name == "main":
code += " Vertex_OUTPUT output;\n"
for stmt in function_node.body:
Expand All @@ -164,7 +165,7 @@ def generate_function(self, node, shader_type):
)
return_type = self.map_type(function_node.return_type)

code += f"{return_type} {function_node.name}({params}) {{\n"
code += f"{return_type} PSMain({params}) {{\n"
if function_node.name == "main":
code += " Fragment_OUTPUT output;\n"
for stmt in function_node.body:
Expand Down Expand Up @@ -281,6 +282,12 @@ def generate_expression(self, expr, shader_type=None):
)
func_name = self.translate_expression(expr.name, shader_type)
return f"{func_name}({args})"
elif isinstance(expr, UnaryOpNode):
return f"{self.map_operator(expr.op)}{self.generate_expression(expr.operand, shader_type)}"

elif isinstance(expr, TernaryOpNode):
return f"{self.generate_expression(expr.condition, shader_type)} ? {self.generate_expression(expr.true_expr, shader_type)} : {self.generate_expression(expr.false_expr, shader_type)}"

elif isinstance(expr, MemberAccessNode):
return f"{self.generate_expression(expr.object, shader_type)}.{expr.member}"
else:
Expand Down
7 changes: 7 additions & 0 deletions src/translator/codegen/metal_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ForNode,
VariableNode,
FunctionCallNode,
TernaryOpNode,
MemberAccessNode,
)

Expand Down Expand Up @@ -327,6 +328,12 @@ def generate_expression(self, expr, shader_type=None):
)
func_name = self.translate_expression(expr.name, shader_type)
return f"{func_name}({args})"

elif isinstance(expr, UnaryOpNode):
return f"{self.map_operator(expr.op)}{self.generate_expression(expr.operand, shader_type)}"

elif isinstance(expr, TernaryOpNode):
return f"{self.generate_expression(expr.condition, shader_type)} ? {self.generate_expression(expr.true_expr, shader_type)} : {self.generate_expression(expr.false_expr, shader_type)}"
elif isinstance(expr, MemberAccessNode):
return f"{self.generate_expression(expr.object, shader_type)}.{expr.member}"
else:
Expand Down
6 changes: 6 additions & 0 deletions src/translator/codegen/opengl_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
MemberAccessNode,
VERTEXShaderNode,
FRAGMENTShaderNode,
TernaryOpNode,
)


Expand Down Expand Up @@ -177,6 +178,11 @@ def generate_expression(self, expr, shader_type=None):
)
func_name = self.translate_expression(expr.name, shader_type)
return f"{func_name}({args})"
elif isinstance(expr, UnaryOpNode):
return f"{self.map_operator(expr.op)}{self.generate_expression(expr.operand, shader_type)}"

elif isinstance(expr, TernaryOpNode):
return f"{self.generate_expression(expr.condition, shader_type)} ? {self.generate_expression(expr.true_expr, shader_type)} : {self.generate_expression(expr.false_expr, shader_type)}"
elif isinstance(expr, MemberAccessNode):
return f"{self.generate_expression(expr.object, shader_type)}.{expr.member}"
else:
Expand Down
5 changes: 3 additions & 2 deletions src/translator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def skip_comments(self):
self.eat(self.current_token[0])

def eat(self, token_type):

if self.current_token[0] == token_type:
self.pos += 1
self.current_token = (
Expand Down Expand Up @@ -365,7 +364,9 @@ def parse_assignment_or_function_call(self):
def parse_variable_declaration(self, type_name):
name = self.current_token[1]
self.eat("IDENTIFIER")

if self.current_token[0] == "DOT":
self.eat("DOT")
self.eat("IDENTIFIER")
if self.current_token[0] == "SEMICOLON":
self.eat("SEMICOLON")
return VariableNode(type_name, name)
Expand Down
Loading

0 comments on commit 4d434ee

Please sign in to comment.