protected override void TranslateFunctionDefinition(List <string> output, FunctionDefinition functionDef) { output.Add(this.CurrentTabIndention); output.Add("public static "); string returnType = "object"; Annotation returnTypeAnnotation = functionDef.GetAnnotation("type"); if (returnTypeAnnotation != null) { returnType = this.CSharpPlatform.GetTypeStringFromAnnotation(returnTypeAnnotation); } output.Add(returnType); output.Add(" "); output.Add("v_" + functionDef.NameToken.Value); output.Add("("); for (int i = 0; i < functionDef.ArgNames.Length; ++i) { if (i > 0) { output.Add(", "); } if (functionDef.ArgAnnotations[i] == null) { output.Add("object "); } else { string argType = functionDef.ArgAnnotations[i].GetSingleArgAsString(null); string type = this.CSharpPlatform.GetTypeStringFromAnnotation(functionDef.ArgAnnotations[i].FirstToken, argType); output.Add(type); output.Add(" "); } output.Add("v_" + functionDef.ArgNames[i].Value); } output.Add(")"); output.Add(this.NL); output.Add(this.CurrentTabIndention); output.Add("{"); output.Add(this.NL); this.CurrentIndention++; Executable[] code = functionDef.Code; if (functionDef.GetAnnotation("omitReturn") != null) { Executable[] newCode = new Executable[code.Length - 1]; Array.Copy(code, newCode, newCode.Length); code = newCode; } this.Translate(output, code); this.CurrentIndention--; output.Add(this.CurrentTabIndention); output.Add("}"); output.Add(this.NL); }
protected override void TranslateFunctionDefinition(List <string> output, FunctionDefinition functionDef) { output.Add(this.CurrentTabIndention); output.Add("\nfunction "); Annotation returnTypeAnnotation = functionDef.GetAnnotation("type"); if (returnTypeAnnotation == null) { throw new ParserException(functionDef.FirstToken, "Need return type."); } string type = returnTypeAnnotation.GetSingleArgAsString(null); if (type == null) { throw new ParserException(functionDef.FirstToken, "Need return type."); } output.Add("v_"); output.Add(functionDef.NameToken.Value); output.Add("("); for (int i = 0; i < functionDef.ArgNames.Length; ++i) { if (i > 0) { output.Add(", "); } Annotation annotation = functionDef.ArgAnnotations[i]; if (annotation == null) { throw new ParserException(functionDef.FirstToken, "Arg needs a type."); } output.Add("$v_" + functionDef.ArgNames[i].Value); } output.Add(") {\n"); this.CurrentIndention++; HashSet <Variable> variablesUsed = new HashSet <Variable>(); foreach (Executable line in functionDef.Code) { line.GetAllVariablesReferenced(variablesUsed); } foreach (string variable in variablesUsed .Select <Variable, string>(v => v.Name) .Where <string>(s => s.ToUpper() == s) .Distinct <string>() .OrderBy <string, string>(s => s)) { output.Add(this.CurrentTabIndention); output.Add("global $v_"); output.Add(variable); output.Add(";\n"); } foreach (Executable line in functionDef.Code) { this.Translate(output, line); } this.CurrentIndention--; output.Add(this.CurrentTabIndention); output.Add("}\n"); }