// For use when we want to cause an interrupt from the wavy~ core, such as IndexOutOfBounds public static void interrupt_wavy_exception(string exception_name, System.Collections.Generic.List <object> args) { // Create a new instance of the exception class WavyObject exception_object = (WavyObject)((Callable)WavyNamespace.get_var_in_namespace(interpreter.local_scope, NAMESPACE_LOCATION, exception_name)).call(interpreter, args); interrupt_wavy_exception(exception_object); }
// Visit a namespace definition public void visit_namespace(NamespaceStmt namespace_stmt) { WavyNamespace wnamespace = new WavyNamespace((string)namespace_stmt.name.value, new Scope(this.local_scope)); // Define the namespace in the local scope this.local_scope.define(wnamespace.name, wnamespace); // Execute the new namespace with the new scope execute_block(namespace_stmt.body, wnamespace.scope); }
public object visit_list(ListExpr list_expr) { List <object> list = new List <object>(); // Evaluate each expression in the list foreach (Expression expr in list_expr.values) { list.Add(evaluate(expr)); } // Create a new list object, with the items as the args Callable list_class = (Callable)(WavyNamespace.get_var_in_namespace(this.local_scope, "list", "List")); // Call the object with the args return(list_class.call(this, new List <object>() { list })); }
// 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)); }
public static void interrupt_wavy_exception(WavyObject exception_object) { // First check if the exception is valid by checking it extends the base Exception WavyClass WavyClass exception = (WavyClass)WavyNamespace.get_var_in_namespace(interpreter.local_scope, NAMESPACE_LOCATION, BASE_EXCEPTION_CLASS); // First check if it extends 'Exception' WavyClass super = exception_object.the_class; while (super != exception && super != null) { super = super.superclass; } if (super != exception) { throw new RuntimeException("Class must be an Exception to be able to interrupt"); } // Then throw the interrupt throw new InterruptException(exception_object, (string)exception_object.get("message")); }
public void visit_using(UsingStmt using_stmt) { // NOTE: This code is literally the same as 'visit_namespace_value', & thus should be optimised // Get the first namespace WavyNamespace next_namespace = (WavyNamespace)evaluate(using_stmt._namespace); // The expression value of the first namespace Expression namespace_value = using_stmt._namespace; 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; } // Finally, set the using namespace scope to the final NamespaceValueExpr value, which is a namespace name in VariableExpr form this.using_namespace = ((WavyNamespace)(next_namespace.scope.get(((VariableExpr)namespace_value).name))).scope; Console.WriteLine("done using!"); }
public void visit_trycatch(TryCatchStmt trycatch_stmt) { try { execute(trycatch_stmt.try_body); }catch (WavyException exception) { WavyFunction function = new WavyFunction(trycatch_stmt.catch_body, this.local_scope, false); // We want to create a new instance of a WavyException here Callable exception_class = (Callable)WavyNamespace.get_var_in_namespace(this.local_scope, "exception", "Exception"); function.call(this, new List <object>() { exception_class.call(this, new List <object>() { "[Caught Exception] " + exception.errror_msg }) } ); } }
// 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); }