// This is assignment of value, lowest precidence private Expression assignment() { Expression higher_precedence = connective_or(); if (consume(Token.Type.Assignment) != null) { Expression assign_value = assignment(); // If we are assigning to a non-object variable if (higher_precedence.GetType() == typeof(VariableExpr)) { VariableExpr var = (VariableExpr)higher_precedence; return(new AssignExpr(var.name, assign_value)); } // If we are assigning to a namespace value else if (higher_precedence.GetType() == typeof(NamespaceValueExpr)) { NamespaceValueExpr var = (NamespaceValueExpr)higher_precedence; return(new AssignNamespaceExpr(var, assign_value)); } // If we are assigning to a member variable else if (higher_precedence.GetType() == typeof(ObjGetExpr)) { ObjGetExpr member = (ObjGetExpr)higher_precedence; return(new ObjSetExpr(member.obj, member.identifier, assign_value)); } } return(higher_precedence); }
public object visit_namespace_value(NamespaceValueExpr namespace_value_expr) { // Needs work as it causes StackOverflow if we are retrieving an invalid identifier // We need some way of checking whether the namespace is valid, and the variable exists in the namespace resolve(namespace_value_expr.namespc); resolve(namespace_value_expr.value); return(null); }
// Get the value from a given namespace & identifier public object visit_namespace_value(NamespaceValueExpr namespace_value_expr) { // Check if the given namespace exists if (this.namespaces.ContainsKey((string)namespace_value_expr.namespc.name.value)) { return(this.namespaces[(string)namespace_value_expr.namespc.name.value].get(namespace_value_expr.identifier)); } throw new RuntimeException("Cannot find namespace '" + (string)namespace_value_expr.namespc.name.value + "'"); }
public object visit_namespace_value(NamespaceValueExpr namespace_value_expr) { if (this.namespaces.ContainsKey((string)namespace_value_expr.namespc.name.value)) { // Save the scope and set the current to the desired namespace Stack <Dictionary <string, bool> > previous_scope = this.scopes; this.scopes = this.namespaces[(string)namespace_value_expr.namespc.name.value]; resolve_local_position(namespace_value_expr, namespace_value_expr.identifier); this.scopes = previous_scope; return(null); } // This causes errors becuase it doesn't account for imports //throw new RuntimeException("Cannot find namespace '" + (string)namespace_value_expr.namespc.name.value + "'"); return(null); }
// Get the value from a given namespace & identifier public object visit_namespace_value(NamespaceValueExpr namespace_value_expr) { // Get the first namespace WavyNamespace next_namespace = (WavyNamespace)evaluate(namespace_value_expr.namespc); // The expression value of the first namespace Expression namespace_value = namespace_value_expr.value; while (namespace_value is NamespaceValueExpr) { // Get the name of the namespace in the nested namespace expression VariableExpr nested_namespace_name = ((VariableExpr)((NamespaceValueExpr)namespace_value).namespc); // Get the next namespace from the scope of the previous next_namespace = (WavyNamespace)next_namespace.scope.get(nested_namespace_name.name); // Set the namespace value to the next value namespace_value = ((NamespaceValueExpr)namespace_value).value; } return(next_namespace.scope.get(((VariableExpr)namespace_value).name)); }
private Expression unary() { while (expect(Token.Type.ConnectiveNot) || expect(Token.Type.BitwiseNot) || expect(Token.Type.Increment) || expect(Token.Type.Decrement)) { // If we have an increment, we want an assign expression if (expect(Token.Type.Increment) || expect(Token.Type.Decrement)) { Token un_operator = consume(); Token op = (un_operator.type == Token.Type.Increment) ? new Token(Token.Type.Plus, null) : new Token(Token.Type.Minus, null); Expression to_crement = mul_div_mod_rem(); // Check the type of the incrementing expression // If we are incrementing to a non-object variable if (to_crement.GetType() == typeof(VariableExpr)) { VariableExpr var = (VariableExpr)to_crement; return(new AssignExpr(var.name, new BinaryExpr(to_crement, op, new LiteralExpr(1)))); } // If we are incrementing a namespace value else if (to_crement.GetType() == typeof(NamespaceValueExpr)) { NamespaceValueExpr var = (NamespaceValueExpr)to_crement; return(new AssignNamespaceExpr(var, new BinaryExpr(to_crement, op, new LiteralExpr(1)))); } // If we are incrementing a member variable else if (to_crement.GetType() == typeof(ObjGetExpr)) { ObjGetExpr member = (ObjGetExpr)to_crement; return(new ObjSetExpr(member.obj, member.identifier, new BinaryExpr(to_crement, op, new LiteralExpr(1)))); } else { throw new ParseExceptionUnexpectedToken("Increment assignment cannot be applied to " + to_crement.GetType()); } } else { Token op = consume(); Expression right = unary(); return(new UnaryExpr(op, right)); } } return(call()); }
private Expression mul_div_mod_rem() { Expression higher_precedence = unary(); while (expect(Token.Type.Multiply) || expect(Token.Type.Divide) || expect(Token.Type.Mod) || expect(Token.Type.NoRemainder)) { Token op = consume(); // Check for compound assignment if (consume(Token.Type.Assignment) != null) { Expression value = unary(); // Check the type of the higher precidence // If we are assigning to a non-object variable if (higher_precedence.GetType() == typeof(VariableExpr)) { VariableExpr var = (VariableExpr)higher_precedence; return(new AssignExpr(var.name, new BinaryExpr(higher_precedence, op, value))); } // If we are assigning to a namespace value else if (higher_precedence.GetType() == typeof(NamespaceValueExpr)) { NamespaceValueExpr var = (NamespaceValueExpr)higher_precedence; return(new AssignNamespaceExpr(var, new BinaryExpr(higher_precedence, op, value))); } // If we are assigning to a member variable else if (higher_precedence.GetType() == typeof(ObjGetExpr)) { ObjGetExpr member = (ObjGetExpr)higher_precedence; return(new ObjSetExpr(member.obj, member.identifier, new BinaryExpr(higher_precedence, op, value))); } else { throw new ParseExceptionUnexpectedToken("Compound assignment cannot be applied to " + higher_precedence.GetType()); } } else { Expression right = unary(); higher_precedence = new BinaryExpr(higher_precedence, op, right); } } return(higher_precedence); }
public AssignNamespaceExpr(NamespaceValueExpr identifier, Expression value) { this.identifier = identifier; this.value = value; }