public Compilation(string sourcePath, string outputPath, string fileName) { string text = System.IO.File.ReadAllText(sourcePath); // test available in relative directory: sourcePath = "...\SAL\Antlr\Test_Parser_src\Test_Parser\Tests" p4Lexer lexer = new p4Lexer(new AntlrInputStream(text)); CommonTokenStream stream = new CommonTokenStream(lexer); p4Parser parser = new p4Parser(stream); IParseTree tree = parser.s(); ASTNode AST = new ConcreteP4Visitor().Visit(tree); //AST?.PrintTrees(0); SymTable s = new SymTable(AST); s.PrintErrors(); TypeCheckVisitor t = new TypeCheckVisitor(AST); t.Errors.ForEach(s => Console.WriteLine(s.Message)); CodeEmitter CodeGenerator = new CodeEmitter(); CodeGenerator.GenerateCode(outputPath, fileName, AST); //test available in relative directory: // outputPath = "...\SAL\Antlr\Test_Parser_src\Test_Parser\Tests" // fileName = "Tests" }
/// <inheritdoc /> public bool CanSee(Vector3 source, IEntity target, bool mapBlocks = true, bool buildingsBlock = true, bool unitsBlock = false) { Vector3 diff = target.Position - source; Ray visionRay = new Ray(source, diff); List <RayQueryResult> results = octree.Raycast(visionRay, RayQueryLevel.Aabb, diff.Length * 1.2f); //distance between source and target with 20% reserve TypeCheckVisitor <IUnit> unitCheck = new TypeCheckVisitor <IUnit>(); TypeCheckVisitor <IBuilding> buildingCheck = new TypeCheckVisitor <IBuilding>(); foreach (var result in results) { // If we hit a map if (Map.IsRaycastToMap(result)) { // If map blocks sight, return false, else check next result if (mapBlocks) { return(false); } continue; } if (!nodeToEntity.TryGetValue(result.Node, out IEntity entity)) { continue; } if (entity == target) { return(true); } // Entity is not target, check if it is unit if (entity.Accept(unitCheck)) { // it is unit, if units block sight, return false, else check next result if (unitsBlock) { return(false); } continue; } // Entity is not a unit, check if it is a building if (entity.Accept(buildingCheck)) { // Entity is a building, if buildings block sight, return false, else check next result if (buildingsBlock) { return(false); } continue; } // it was not the map, nor unit, nor building, just check next, ignore } return(false); }
public IType GetExpressionType(IExpressionNode node) { if (!_expressionResultTypes.TryGetValue(node, out IType type)) { TypeCheckVisitor visitor = new TypeCheckVisitor(_context, _typeManager, _errors, this); node.AcceptExpressionVisitor(visitor); type = visitor.Type; _expressionResultTypes[node] = type; } return(type); }
void VerifyElements() { var tcv = new TypeCheckVisitor(); var csv = new CallStatementVisitor(); foreach (var element in Elements) { element.AcceptVisitor(tcv); } foreach (var call in Elements.Where(ele => ele is CallStatement)) { call.AcceptVisitor(csv); } }
public static CompilationResult Compile(CompilationJob compilationJob) { var results = new List <CompilationResult>(); // All variable declarations that we've encountered during this // compilation job var derivedVariableDeclarations = new List <Declaration>(); // All variable declarations that we've encountered, PLUS the // ones we knew about before var knownVariableDeclarations = new List <Declaration>(); if (compilationJob.VariableDeclarations != null) { knownVariableDeclarations.AddRange(compilationJob.VariableDeclarations); } // Get function declarations from the library, if provided if (compilationJob.Library != null) { knownVariableDeclarations.AddRange(GetDeclarationsFromLibrary(compilationJob.Library)); } var compiledTrees = new List <(string name, IParseTree tree)>(); // First pass: parse all files, generate their syntax trees, // and figure out what variables they've declared var stringTableManager = new StringTableManager(); foreach (var file in compilationJob.Files) { var tree = ParseSyntaxTree(file); compiledTrees.Add((file.FileName, tree)); RegisterStrings(file.FileName, stringTableManager, tree); } if (compilationJob.CompilationType == CompilationJob.Type.StringsOnly) { // Stop at this point return(new CompilationResult { Declarations = null, ContainsImplicitStringTags = stringTableManager.ContainsImplicitStringTags, Program = null, StringTable = stringTableManager.StringTable, }); } var fileTags = new Dictionary <string, IEnumerable <string> >(); foreach (var parsedFile in compiledTrees) { GetDeclarations(parsedFile.name, parsedFile.tree, knownVariableDeclarations, out var newDeclarations, out var newFileTags); derivedVariableDeclarations.AddRange(newDeclarations); knownVariableDeclarations.AddRange(newDeclarations); fileTags.Add(parsedFile.name, newFileTags); } foreach (var parsedFile in compiledTrees) { var checker = new TypeCheckVisitor(parsedFile.name, knownVariableDeclarations); checker.Visit(parsedFile.tree); derivedVariableDeclarations.AddRange(checker.NewDeclarations); knownVariableDeclarations.AddRange(checker.NewDeclarations); } if (compilationJob.CompilationType == CompilationJob.Type.DeclarationsOnly) { // Stop at this point return(new CompilationResult { Declarations = derivedVariableDeclarations, ContainsImplicitStringTags = false, Program = null, StringTable = null, FileTags = fileTags, }); } foreach (var parsedFile in compiledTrees) { CompilationResult compilationResult = GenerateCode(parsedFile.name, knownVariableDeclarations, compilationJob, parsedFile.tree, stringTableManager); results.Add(compilationResult); } var finalResult = CompilationResult.CombineCompilationResults(results, stringTableManager); // Last step: take every variable declaration we found in all // of the inputs, and create an initial value registration for // it. foreach (var declaration in knownVariableDeclarations) { // We only care about variable declarations here if (declaration.DeclarationType != Declaration.Type.Variable) { continue; } Operand value; if (declaration.DeclarationType == Declaration.Type.Variable && declaration.defaultValue == null) { throw new NullReferenceException($"Variable declaration {declaration.name} ({declaration.ReturnType}) has a null default value. This is not allowed."); } switch (declaration.ReturnType) { case Yarn.Type.Number: value = new Operand(Convert.ToSingle(declaration.DefaultValue)); break; case Yarn.Type.String: value = new Operand(Convert.ToString(declaration.DefaultValue)); break; case Yarn.Type.Bool: value = new Operand(Convert.ToBoolean(declaration.DefaultValue)); break; default: throw new ArgumentOutOfRangeException($"Cannot create an initial value for type {declaration.ReturnType}"); } finalResult.Program.InitialValues.Add(declaration.Name, value); } finalResult.Declarations = derivedVariableDeclarations; finalResult.FileTags = fileTags; return(finalResult); }