Skip to content

Commit

Permalink
Merge pull request #24 from samthakur587/translater_fix
Browse files Browse the repository at this point in the history
fix: small fix in translate class
  • Loading branch information
vaatsalya123 authored Jul 31, 2024
2 parents 79ff160 + 7a61acb commit efb9256
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 18 deletions.
19 changes: 9 additions & 10 deletions crosstl/_crosstl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
from .src.translator.ast import ASTNode
from .src.backend.DirectX import *
from .src.backend.Metal import *
#from .src.backend.Opengl import *
from .src.backend.Opengl import *

def translate(file_path: str, backend: str = "cgl") -> str:

def translate(file_path: str, backend: str = "crossgl") -> str:
backend = backend.lower()

with open(file_path, 'r') as file:
with open(file_path, "r") as file:
shader_code = file.read()

# Determine the input shader type based on the file extension
if file_path.endswith(".cgl"):
from .src.translator.lexer import Lexer
from .src.translator.parser import Parser
lexer = Lexer(shader_code)
parser = Parser(lexer.tokens)
elif file_path.endswith(".hlsl"):
Expand All @@ -26,9 +25,8 @@ def translate(file_path: str, backend: str = "cgl") -> str:
lexer = MetalLexer(shader_code)
parser = MetalParser(lexer.tokens)
elif file_path.endswith(".glsl"):
from .src.backend.Opengl import Lexer , Parser
lexer = Lexer(shader_code)
parser = Parser(lexer.tokens)
lexer = GLSLLexer(shader_code)
parser = GLSLParser(lexer.tokens)
else:
raise ValueError(f"Unsupported shader file type: {file_path}")

Expand All @@ -53,10 +51,11 @@ def translate(file_path: str, backend: str = "cgl") -> str:
elif file_path.endswith(".metal"):
codegen = MetalToCrossGLConverter()
elif file_path.endswith(".glsl"):
from .src.backend.Opengl import CrossglCodeGen as GLSLToCrossGLConverter
codegen = GLSLToCrossGLConverter()
else:
raise ValueError(f"Reverse translation not supported for: {file_path}")
return codegen.generate(ast)
else:
raise ValueError(f"Unsupported translation scenario: {file_path} to {backend}")
raise ValueError(
f"Unsupported translation scenario: {file_path} to {backend}"
)
2 changes: 1 addition & 1 deletion crosstl/src/backend/DirectX/DirectxCrossGLCodeGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self):
"int": "int",
}

def convert(self, ast):
def generate(self, ast):
self.process_structs(ast)

code = "shader main {\n"
Expand Down
2 changes: 1 addition & 1 deletion crosstl/src/backend/Opengl/OpenglLexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
}


class Lexer:
class GLSLLexer:
def __init__(self, code):
self.code = code
self.tokens = []
Expand Down
4 changes: 2 additions & 2 deletions crosstl/src/backend/Opengl/OpenglParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
FRAGMENTShaderNode,
VersionDirectiveNode,
)
from .OpenglLexer import Lexer
from .OpenglLexer import *


class Parser:
class GLSLParser:
def __init__(self, tokens):
self.tokens = tokens
self.pos = 0
Expand Down
6 changes: 3 additions & 3 deletions crosstl/src/backend/Opengl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .OpenglLexer import Lexer
from .OpenglParser import Parser
from .openglCrossglCodegen import CrossglCodeGen
from .OpenglLexer import *
from .OpenglParser import *
from .openglCrossglCodegen import *
2 changes: 1 addition & 1 deletion crosstl/src/backend/Opengl/openglCrossglCodegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .OpenglLexer import *


class CrossglCodeGen:
class GLSLToCrossGLConverter:
def __init__(self):
self.current_shader = None
self.vertex_item = None
Expand Down

0 comments on commit efb9256

Please sign in to comment.