public FunctionDefinition FromSPAGS(SPAGS.Function spagsFunc) { FunctionDefinition jsFunc = new FunctionDefinition(); for (int i = 0; i < spagsFunc.ParameterVariables.Count; i++) { SPAGS.Parameter spagsParam = spagsFunc.ParameterVariables[i]; Variable p = new Variable(spagsParam.Name, GetValueTypes(spagsParam.Type)); AddReference(spagsParam, p); jsFunc.Parameters.Add(p); } foreach (SPAGS.Statement statement in spagsFunc.Body.ChildStatements) { jsFunc.Body.Add(FromSPAGS(spagsFunc, statement, jsFunc.Body)); } return jsFunc; }
public Statement FromSPAGS(SPAGS.Function spagsFunc, SPAGS.Statement spagsStatement, ScopedBlock jsScope) { SPAGS.Function callFunction; List<SPAGS.Expression> callParams; List<SPAGS.Expression> callVarargs; if (spagsStatement.TryGetSimpleCall(out callFunction, out callParams, out callVarargs)) { return (Statement)FromSPAGS(callFunction, callParams, callVarargs); } switch (spagsStatement.Type) { case SPAGS.StatementType.Block: SPAGS.Statement.Block spagsBlock = (SPAGS.Statement.Block)spagsStatement; Statement.GenericBlock jsBlock = new Statement.GenericBlock(); foreach (SPAGS.Statement statement in spagsBlock.ChildStatements) { jsBlock.Block.Add(FromSPAGS(spagsFunc, statement, jsScope)); } return jsBlock; case SPAGS.StatementType.Assign: SPAGS.Statement.Assign spagsAssign = (SPAGS.Statement.Assign)spagsStatement; PossibleValueTypes assignType = GetValueTypes(spagsAssign.Target.GetValueType()); Expression jsAssign = new Expression.InfixOperation( FromSPAGS(spagsAssign.Target), Infix.Assign, FromSPAGS(spagsAssign.SimpleAssignValue()).Cast(assignType)); return (Statement)jsAssign; case SPAGS.StatementType.If: SPAGS.Statement.If spagsIf = (SPAGS.Statement.If)spagsStatement; Statement.If jsIf = new Statement.If( FromSPAGS(spagsIf.IfThisIsTrue).Cast(PossibleValueTypes.Boolean)); if (spagsIf.ThenDoThis.Type == SPAGS.StatementType.Block) { SPAGS.Statement.Block thenBlock = (SPAGS.Statement.Block)spagsIf.ThenDoThis; foreach (SPAGS.Statement statement in thenBlock.ChildStatements) { jsIf.ThenDoThis.Add(FromSPAGS(spagsFunc, statement, jsScope)); } } else { jsIf.ThenDoThis.Add(FromSPAGS(spagsFunc, spagsIf.ThenDoThis, jsScope)); } if (spagsIf.ElseDoThis != null) { if (spagsIf.ElseDoThis.Type == SPAGS.StatementType.Block) { SPAGS.Statement.Block elseBlock = (SPAGS.Statement.Block)spagsIf.ElseDoThis; foreach (SPAGS.Statement statement in elseBlock.ChildStatements) { jsIf.ElseDoThis.Add(FromSPAGS(spagsFunc, statement, jsScope)); } } else { jsIf.ElseDoThis.Add(FromSPAGS(spagsFunc, spagsIf.ElseDoThis, jsScope)); } } return jsIf; case SPAGS.StatementType.Return: SPAGS.Statement.Return spagsReturn = (SPAGS.Statement.Return)spagsStatement; if (spagsReturn.Value == null) { if (spagsFunc.Signature.ReturnType.Category == SPAGS.ValueTypeCategory.Void) { return new Statement.Return(); } else { return new Statement.Return(FromSPAGS(spagsFunc.Signature.ReturnType.CreateDefaultValueExpression())); } } else { PossibleValueTypes returnType = GetValueTypes(spagsFunc.Signature.ReturnType); return new Statement.Return(FromSPAGS(spagsReturn.Value).Cast(returnType)); } case SPAGS.StatementType.VariableDeclaration: SPAGS.Statement.VariableDeclaration varDef = (SPAGS.Statement.VariableDeclaration)spagsStatement; Statement.InitVariables assignment = new Statement.InitVariables(); foreach (SPAGS.Variable variable in varDef.Variables) { Variable v; if (jsScope.Variables.ContainsKey(variable.Name)) { v = jsScope.Variables[variable.Name]; } else { v = new Variable(variable.Name, GetValueTypes(variable.Type)); jsScope.Variables.Add(variable.Name, v); } SetReference(variable, v); SPAGS.Expression val = variable.InitialValue ?? variable.Type.CreateDefaultValueExpression(); PossibleValueTypes variableType = GetValueTypes(variable.Type); if (val.IsConstant()) { v.InitialValue = FromSPAGS(val).Cast(variableType); } else { assignment.Add(v, FromSPAGS(val).Cast(variableType)); } } return assignment; case SPAGS.StatementType.While: SPAGS.Statement.While spagsLoop = (SPAGS.Statement.While)spagsStatement; Statement.While jsLoop = new Statement.While( FromSPAGS(spagsLoop.WhileThisIsTrue).Cast(PossibleValueTypes.Boolean)); if (spagsLoop.KeepDoingThis.Type == SPAGS.StatementType.Block) { SPAGS.Statement.Block body = (SPAGS.Statement.Block)spagsLoop.KeepDoingThis; foreach (SPAGS.Statement statement in body.ChildStatements) { jsLoop.KeepDoingThis.Add(FromSPAGS(spagsFunc, statement, jsScope)); } } else { jsLoop.KeepDoingThis.Add(FromSPAGS(spagsFunc, spagsLoop.KeepDoingThis, jsScope)); } return jsLoop; } return (Statement)(new Expression.Custom("(" + spagsStatement.Type + ")")); }