private static void PrintTree(string codePath, TextWriter output) { var parser = new AdamantParser(codePath) { BuildParseTree = true }; var tree = parser.compilationUnit(); var diagnostics = new ParseDiagnosticsBuilder(new SourceFile(new FileInfo(codePath))); var syntaxCheck = new SyntaxCheckVisitor(diagnostics); tree.Accept(syntaxCheck); // TODO print syntax check errors output.WriteLine(tree.ToStringTree(parser)); }
public override MainFunctions VisitConstructor(AdamantParser.ConstructorContext context) { constructorCount++; O.BlankLine(); var constructorName = context.name?.GetText(); if(constructorName != null) { O.WriteIndentedLine($"public partial class {constructorName}"); O.BeginBlock(); } O.WriteIndented(Format(context.modifier()) + "static "); if(context.returnType != null) context.returnType.Accept(this); else O.Write(CurrentClassName); O.Write(" אCtor"); // TODO handle generic class parameters O.Write("("); O.WriteList(context.parameterList()._parameters, this); O.Write(")"); O.WriteLine(); // Body O.BeginBlock(); O.WriteIndented("return new "); if(context.returnType != null) context.returnType.Accept(this); // TODO handle the case where this is a base type else O.Write(CurrentClassName); O.Write("("); if(constructorName != null) { O.Write("default(אCtorName_"); O.Write(constructorName); O.Write("), "); } O.Write(string.Join(", ", context.parameterList()._parameters.Select(p => p.name.GetText()))); O.WriteLine(");"); O.EndBlock(); if(constructorName != null) O.EndBlock(); return MainFunctions.Empty; }
public override MainFunctions VisitClassDeclaration(AdamantParser.ClassDeclarationContext context) { O.BlankLine(); O.WriteIndented(Format(context.modifier().Where(IsAccessModifier)) + $"partial class {CurrentClassName}"); O.WriteLine(); O.BeginBlock(); context.member().Select(m => m.Accept(this)).Combine(); if(constructorCount == 0 && !IsAbstract(context.modifier())) { // Generate default constructor O.WriteIndentedLine($"public static {CurrentClassName} אCtor()"); O.BeginBlock(); O.WriteIndentedLine($"return new {CurrentClassName}();"); O.EndBlock(); } O.EndBlock(); return MainFunctions.Empty; }
private static MainFunctions Compile(string codePath, TextWriter output) { var stream = new AntlrFileStream(codePath); var lexer = new AdamantLexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new AdamantParser(tokens) { BuildParseTree = true }; var tree = parser.compilationUnit(); var syntaxCheck = new SyntaxCheckVisitor(); tree.Accept(syntaxCheck); //var buildAst = new BuildAstVisitor(); //var ast = (Assemblage)tree.Accept(buildAst); //var borrowChecker = new BorrowChecker(); //borrowChecker.Check(ast); var cSharpGenerator = new CSharpGenerator(output); return tree.Accept(cSharpGenerator); }
private static void PrintTree(string codePath, string outputPath) { var output = outputPath != null ? File.CreateText(outputPath) : Console.Out; var stream = new AntlrFileStream(codePath); var lexer = new AdamantLexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new AdamantParser(tokens) { BuildParseTree = true }; var tree = parser.compilationUnit(); var syntaxCheck = new SyntaxCheckVisitor(); tree.Accept(syntaxCheck); output.WriteLine(tree.ToStringTree(parser)); }
public override MainFunctions VisitField(AdamantParser.FieldContext context) { return MainFunctions.Empty; }
public override MainFunctions VisitMethod(AdamantParser.MethodContext context) { return MainFunctions.Empty; }