Beispiel #1
0
 private void assign(Assign node)
 {
     scope.set((Variable)node.children[0], eval(node.children[1]));
 }
Beispiel #2
0
 private ASTNode parseStatement(SyntaxNode node)
 {
     if (!node.token.Equals(grammar.t["stmt"]))
     {
         throw new Exception("Trying to parse a non-statement " + node.token + " as a statement, line " + node.line + ":" + node.position);
     }
     if (node.children[0].token.Equals(grammar.t["var"]))
     {
         Define   ret         = new Define(node);
         Variable newVariable = parseVariable(node.children[1], true);
         TypeName type        = parseType(node.children[3]);
         ret.children.Add(newVariable);
         ret.children.Add(type);
         newVariable.type = type.type;
         if (node.children[4].children.Count > 0)
         {
             Expression initialValue = parseExpression(node.children[4].children[1]);
             if (initialValue.type != newVariable.type)
             {
                 addError(node, "Variable " + newVariable.name + " defined with initial value of different type");
             }
             ret.children.Add(initialValue);
         }
         else
         {
             ret.children.Add(null);
         }
         return(ret);
     }
     if (node.children[0].token is Identifier)
     {
         Assign   ret = new Assign(node);
         Variable var = parseVariable(node.children[0], false);
         if (currentConstants.Contains(var.name))
         {
             addError(node, "Assignment to loop variable " + var.name);
         }
         Expression value = parseExpression(node.children[2]);
         if (var.type != value.type)
         {
             addError(node, "Variable " + var.name + " assigned a value of different type");
         }
         ret.children.Add(var);
         ret.children.Add(value);
         return(ret);
     }
     if (node.children[0].token.Equals(grammar.t["for"]))
     {
         For        ret  = new For(node);
         Variable   var  = parseVariable(node.children[1], false);
         Expression from = parseExpression(node.children[3]);
         Expression to   = parseExpression(node.children[5]);
         if (var.type != typeof(int))
         {
             addError(node, "Only ints may be iterated on");
         }
         if (from.type != typeof(int))
         {
             addError(node, "Iteration start needs to be an int");
         }
         if (to.type != typeof(int))
         {
             addError(node, "Iteration end needs to be an int");
         }
         ret.children.Add(var);
         ret.children.Add(from);
         ret.children.Add(to);
         currentConstants.Add(var.name);
         ret.children.Add(parse(node.children[7]));
         ret.children.Add(parse(node.children[9]));
         currentConstants.Remove(var.name);
         return(ret);
     }
     if (node.children[0].token.Equals(grammar.t["read"]))
     {
         Read ret = new Read(node);
         ret.children.Add(parseVariable(node.children[1], false));
         return(ret);
     }
     if (node.children[0].token.Equals(grammar.t["print"]))
     {
         Print ret = new Print(node);
         ret.children.Add(parseExpression(node.children[1]));
         return(ret);
     }
     if (node.children[0].token.Equals(grammar.t["assert"]))
     {
         Assertion ret = new Assertion(node);
         ret.children.Add(parseExpression(node.children[2]));
         return(ret);
     }
     throw new Exception("Un-AST-ed syntax node, token " + node.token + ", first child " + node.children[0].token + ", line " + node.line + ":" + node.position);
 }