def get_js_vector_operator_expression(glsl_operand1, glsl_operand2, operator): return pypeg2js.PostfixExpression([ *glsl_operand1.content, pypeg2js.BracketedExpression( pypeg2js.PostfixExpression([f"'{operator}'"])), pypeg2js.InvocationExpression( pypeg2js.PostfixExpression(glsl_operand2.content)) ])
def get_js_invocation_expression(glsl_expression, scope): js_expression = js.PostfixExpression() js_invocation = js.InvocationExpression( [get_js(argument, scope) for argument in glsl_expression.arguments] ) glsl_reference = glsl_expression.reference if glsl_reference in js_glm_library_functions: return js.PostfixExpression(['glm', glsl_reference, js_invocation]) elif glsl_reference in js_math_library_functions: return js.PostfixExpression(['Math', glsl_reference, js_invocation]) else: return js.PostfixExpression([get_js(glsl_reference, scope), js_invocation])
def get_js_attribute_expression(glsl_expression, scope): if isinstance(glsl_expression.reference, glsl.InvocationExpression): js_expression = get_js(glsl_expression.reference, scope) else: js_expression = js.PostfixExpression([get_js(glsl_expression.reference, scope)]) for attribute in glsl_expression.attributes: js_expression.content.append(get_js(attribute, scope)) return js_expression
def get_js_structure_declaration(glsl_structure, scope): js_function = js.FunctionDeclaration(glsl_structure.name) js_object = js.AssociativeListExpression() js_object.content = [] for glsl_declaration in glsl_structure.content: for element in glsl_declaration.content: name = element if isinstance(element, str) else element.operand1 js_function.parameters.append(js.ParameterDeclaration(name, f'/*{glsl_declaration.type}*/')) js_object.content.append(js.AttributeDeclaration(name,js.PostfixExpression([name]) )) js_function.content.append( js.ReturnStatement( js.PostfixExpression([js_object]) ) ) return js_function
def get_js_unary_glm_operator_expression(glsl_operand1, operator, scope): type1 = scope.deduce_type(glsl_operand1) assert operator != '!', 'Unary negation for vectors/matrices is not supported by glm-js, cannot safely continue' if operator == '++': return js.PostfixExpression([ js.ParensExpression([get_js(glsl_operand1, scope)]), peg.parse('["+="]', js.BracketedExpression), js.InvocationExpression(js.PostfixExpression(['glm', *type1, js.InvocationExpression(['1'])])) ]) elif operator == '--': return js.PostfixExpression([ js.ParensExpression([get_js(glsl_operand1, scope)]), peg.parse('["-="]', js.BracketedExpression), js.InvocationExpression(js.PostfixExpression(['glm', *type1, js.InvocationExpression(['1'])])) ]) elif operator == '-' and isinstance(glsl_operand1, glsl.AttributeExpression): return js.PostfixExpression([ glsl_operand1.reference, *glsl_operand1.attributes, peg.parse('["*"]', js.BracketedExpression), peg.parse('(-1)', js.InvocationExpression) ]) elif operator == '-' and isinstance(glsl_operand1, glsl.InvocationExpression): return js.PostfixExpression([ glsl_operand1.reference, glsl_operand1.arguments, peg.parse('["*"]', js.BracketedExpression), peg.parse('(-1)', js.InvocationExpression) ]) elif operator == '-': return js.PostfixExpression([ js.ParensExpression([get_js(glsl_operand1, scope)]), peg.parse('["*"]', js.BracketedExpression), peg.parse('(-1)', js.InvocationExpression) ]) elif operator == '+': return js.ParensExpression([get_js(glsl_operand1, scope)]) else: raise ValueError(f'Unknown unary operator for vector/matrix, cannot safely continue: \n\t{glsl_operand1.compose()}')
def get_js_binary_glm_operator_expression(glsl_operand1, glsl_operand2, operator, scope): if (isinstance(glsl_operand1, glsl.AttributeExpression) or isinstance(glsl_operand1, glsl.InvocationExpression)): return js.PostfixExpression([ *get_js(glsl_operand1, scope).content, js.BracketedExpression(js.PostfixExpression([f"'{operator}'"])), js.InvocationExpression(get_js(glsl_operand2, scope)) ]) elif isinstance(glsl_operand1, str): return js.PostfixExpression([ glsl_operand1, js.BracketedExpression(js.PostfixExpression([f"'{operator}'"])), js.InvocationExpression(get_js(glsl_operand2, scope)) ]) else: return js.PostfixExpression([ js.ParensExpression([get_js(glsl_operand1, scope)]), js.BracketedExpression(js.PostfixExpression([f"'{operator}'"])), js.InvocationExpression(get_js(glsl_operand2, scope)) ])