Example #1
0
 public object visit_namespace_assign(AssignNamespaceExpr assign_namespace_expr)
 {
     // Needs work as it causes StackOverflow if we are assigning to an invalid identifier
     resolve(assign_namespace_expr.identifier);
     resolve(assign_namespace_expr.value);
     return(null);
 }
 public object visit_namespace_assign(AssignNamespaceExpr assign_namespace_expr)
 {
     if (this.namespaces.ContainsKey((string)assign_namespace_expr.identifier.namespc.name.value))
     {
         // Save the old scope and set the current to the new namespace scope
         Stack <Dictionary <string, bool> > previous_scope = this.scopes;
         this.scopes = this.namespaces[(string)assign_namespace_expr.identifier.namespc.name.value];
         resolve(assign_namespace_expr.value);
         // Resolve the variable to the interpreter
         resolve_local_position(assign_namespace_expr, assign_namespace_expr.identifier.identifier);
         this.scopes = previous_scope;
         return(null);
     }
     throw new RuntimeException("Namespace with name '" + (string)assign_namespace_expr.identifier.namespc.name.value + "' doesn't exist");
 }
 // When assigning to a value in a specific namespace
 public object visit_namespace_assign(AssignNamespaceExpr assign_namespace_expr)
 {
     // Check the given namespace exists
     if (this.namespaces.ContainsKey((string)assign_namespace_expr.identifier.namespc.name.value))
     {
         object value = evaluate(assign_namespace_expr.value);
         // Check the namespace value exists
         if (local_depths.ContainsKey(assign_namespace_expr))
         {
             // Save the old scope and set the current to the new namespace scope
             Scope previous_scope = this.local_scope;
             this.local_scope = this.namespaces[(string)assign_namespace_expr.identifier.namespc.name.value];
             // Assign the value in the scope at the given depth in the new namespace
             int distance = local_depths[assign_namespace_expr];
             local_scope.assign_at(distance, assign_namespace_expr.identifier.identifier, value);
             // Restore the namespace
             this.local_scope = previous_scope;
             return(value);
         }
     }
     throw new RuntimeException("Namespace with name '" + (string)assign_namespace_expr.identifier.namespc.name.value + "' doesn't exist");
 }
    // When assigning to a value in a specific namespace
    public object visit_namespace_assign(AssignNamespaceExpr assign_namespace_expr)
    {
        // NOTE: This code is literally the same as 'visit_namespace_value', & thus should be optimised
        // Get the first namespace
        WavyNamespace next_namespace = (WavyNamespace)evaluate(assign_namespace_expr.identifier.namespc);
        // The expression value of the first namespace
        Expression namespace_value = assign_namespace_expr.identifier.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;
        }
        object value = evaluate(assign_namespace_expr.value);

        // Assign the value to the evaluated expression value
        next_namespace.scope.assign(((VariableExpr)namespace_value).name, value);
        return(value);
    }