public bool VisitNode(Variable node) { throw new NotImplementedException(); }
public VariableDeclaration TryParseLocalVarDecl() { Func<ASTNode> declarationParser = () => { if (Tokens.ConsumeToken(TokenType.LocalVariable) == null) return null; ASTNode type = TryParseType(); if (type == null) return Error("Expected variable type!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); if (!Symbols.TryGetSymbol((type as VariableType).Name, out type, OuterClassScope)) return Error("The type '" + (type as VariableType).Name + "' does not exist in the current scope!", type.StartPos, type.EndPos); var vars = ParseVariableNames(); if (vars == null) return Error("Malformed variable names!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1)); foreach (VariableIdentifier ident in vars) { if (Symbols.SymbolExistsInCurrentScope(ident.Name)) return Error("A variable named '" + ident.Name + "' already exists in this scope!", ident.StartPos, ident.EndPos); Variable variable = new Variable(new List<Specifier>(), ident, type as VariableType, ident.StartPos, ident.EndPos); Symbols.AddSymbol(variable.Name, variable); NodeVariables.Locals.Add(variable); variable.Outer = Node; } return new VariableDeclaration(type as VariableType, null, vars, vars.First().StartPos, vars.Last().EndPos); }; return (VariableDeclaration)Tokens.TryGetTree(declarationParser); }
public bool VisitNode(VariableDeclaration node) { ASTNode nodeType; if (node.VarType.Type == ASTNodeType.Struct || node.VarType.Type == ASTNodeType.Enumeration) { // Check type, if its a struct or enum, visit that first. node.VarType.Outer = node.Outer; Success = Success && node.VarType.AcceptVisitor(this); // Add the type to the list of types in the class. NodeUtils.GetContainingClass(node).TypeDeclarations.Add(node.VarType); nodeType = node.VarType; } else if (!Symbols.TryGetSymbol(node.VarType.Name, out nodeType, NodeUtils.GetOuterClassScope(node))) { return Error("No type named '" + node.VarType.Name + "' exists in this scope!", node.VarType.StartPos, node.VarType.EndPos); } else if (!typeof(VariableType).IsAssignableFrom(nodeType.GetType())) { return Error("Invalid variable type, must be a class/struct/enum/primitive.", node.VarType.StartPos, node.VarType.EndPos); } if (node.Outer.Type == ASTNodeType.Class) { int index = NodeUtils.GetContainingClass(node).VariableDeclarations.IndexOf(node); foreach (VariableIdentifier ident in node.Variables) { if (Symbols.SymbolExistsInCurrentScope(ident.Name)) return Error("A member named '" + ident.Name + "' already exists in this class!", ident.StartPos, ident.EndPos); Variable variable = new Variable(node.Specifiers, ident, nodeType as VariableType, ident.StartPos, ident.EndPos); variable.Outer = node.Outer; Symbols.AddSymbol(variable.Name, variable); NodeUtils.GetContainingClass(node).VariableDeclarations.Insert(index++, variable); } NodeUtils.GetContainingClass(node).VariableDeclarations.Remove(node); } else if (node.Outer.Type == ASTNodeType.Struct) { int index = (node.Outer as Struct).Members.IndexOf(node); foreach (VariableIdentifier ident in node.Variables) { if (Symbols.SymbolExistsInCurrentScope(ident.Name)) return Error("A member named '" + ident.Name + "' already exists in this struct!", ident.StartPos, ident.EndPos); Variable variable = new Variable(node.Specifiers, ident, nodeType as VariableType, ident.StartPos, ident.EndPos); variable.Outer = node.Outer; Symbols.AddSymbol(variable.Name, variable); (node.Outer as Struct).Members.Insert(index++, variable); } (node.Outer as Struct).Members.Remove(node); } return Success; }
public bool VisitNode(Variable node) { String type = "ERROR"; if (node.Outer.Type == ASTNodeType.Class || node.Outer.Type == ASTNodeType.Struct) type = "var"; else if (node.Outer.Type == ASTNodeType.Function) type = "local"; // var|local [specifiers] variabletype variablename[[staticarraysize]]; Write("{0} ", type); if (node.Specifiers.Count > 0) Append("{0} ", String.Join(" ", node.Specifiers.Select(x => x.Value))); String staticarray = node.IsStaticArray ? "[" + node.Size + "]" : ""; Append("{0} {1};", node.VarType.Name, node.Name + staticarray); return true; }