public wolFunction GetStaticMethod(string name) { wolFunction func = null; try { func = methods[name]; } catch (KeyNotFoundException) { VirtualMachine.ThrowVMException($"Method by name {name} not found in {strtype}", VirtualMachine.position, ExceptionType.NotFoundException); } try { if (func.arguments["this"] == this) { VirtualMachine.ThrowVMException($"Method by name {name} in {strtype} is not static", VirtualMachine.position, ExceptionType.InitilizateException); } } catch (KeyNotFoundException) { //so ok } return(func); }
public Value(wolClass wolclass, string constr_name, params Value[] arguments) : this(wolclass, SecurityModifer.PUBLIC) { if (wolclass.classType == wolClassType.STRUCT) { //pass } else if (wolclass.classType == wolClassType.ENUM) { //pass } else { if (wolclass is wolDouble) { //pass } else if (wolclass is wolString) { //pass } else { try { Script.Parse(type.constructors[constr_name].body); } catch (KeyNotFoundException) { VirtualMachine.ThrowVMException($"Constructor by name {constr_name} not found in {wolclass}", VirtualMachine.position, ExceptionType.NotFoundException); } } } }
public void ParseBool(string val) { if (!bool.TryParse(val, out value)) { VirtualMachine.ThrowVMException($"'{val}' is not bool", VirtualMachine.position, ExceptionType.FormatException); } }
public void CallDestructor(int index, params Value[] args) { try { destructors[index].Call(args); } catch (IndexOutOfRangeException) { VirtualMachine.ThrowVMException("Index in stack of destructor is bigger than count of destructors", VirtualMachine.position, ExceptionType.IndexOutOfRangeException); } }
/// <summary> /// Get field from type of this value /// </summary> /// <param name="name">Name of field</param> /// <returns></returns> public Value GetField(string name) { try { return(type.fields[name]); } catch (KeyNotFoundException) { VirtualMachine.ThrowVMException($"Field by name {name} not found", VirtualMachine.position, ExceptionType.NotFoundException); return(null); } }
public Value GetStaticField(string name) { Value field = null; try { field = static_fields[name]; } catch (KeyNotFoundException) { VirtualMachine.ThrowVMException($"In class '{strtype}' not found static field by name '{name}'", VirtualMachine.position, ExceptionType.NotFoundException); } return(field); }
public Value Call(params Value[] args) { Dictionary <string, Value> arguments = new Dictionary <string, Value>(); int i = 0; try { foreach (string name in value.arguments.Keys) { arguments.Add(name, args[i]); i++; } return(Script.Parse(value.body, arguments)); } catch (NullReferenceException) { VirtualMachine.ThrowVMException("Function not found in 'Func' structure", VirtualMachine.position, ExceptionType.NullRefrenceException); return(Value.VoidValue); } }
/// <summary> /// Get not static method in this value /// </summary> /// <param name="name">Name of not static method</param> /// <returns></returns> public wolFunction GetMethod(string name) { wolFunction meth = new wolFunction(); //create empty funciton for that compiler don`t get error try { meth = type.methods[name]; } catch (KeyNotFoundException) { VirtualMachine.ThrowVMException("Method by name not found", VirtualMachine.position, ExceptionType.NotFoundException); } if (meth.arguments.ContainsKey("this")) { return(meth); //check on 'static' } else { return(null); } }
public wolClass ToParentClass(string parent_name) { wolClass parent = null; try { parent = parents[parent_name]; } catch (KeyNotFoundException) { VirtualMachine.ThrowVMException($"Parent by name '{parent_name}' not found of '{strtype}'", VirtualMachine.position, ExceptionType.NotFoundException); } if (parent.classType != wolClassType.ENUM) { foreach (KeyValuePair <string, wolFunction> method in methods) { if (!method.Value.close) { try { parent.methods.Add(method.Key, method.Value); } catch (ArgumentException) { continue; } } } foreach (KeyValuePair <string, Value> field in fields) { try { parent.fields.Add(field.Key, field.Value); } catch (ArgumentException) { continue; } } } if ((parent.classType == wolClassType.DEFAULT) || (parent.classType == wolClassType.STRUCT)) { foreach (KeyValuePair <string, wolFunction> constructor in constructors) { if (!constructor.Value.close) { try { parent.constructors.Add(constructor.Key, constructor.Value); } catch (ArgumentException) { continue; } } } foreach (wolFunction destructor in destructors) { if (!destructor.close) { try { parent.destructors.Add(destructor); } catch (ArgumentException) { continue; } } } } if ((parent.classType == wolClassType.ENUM) || (parent.classType == wolClassType.STRUCT)) { foreach (KeyValuePair <string, Value> constant in parent.constants) { try { parent.constants.Add(constant.Key, constant.Value); } catch (ArgumentException) { continue; } } } else { foreach (KeyValuePair <string, Value> stfield in static_fields) { try { parent.static_fields.Add(stfield.Key, stfield.Value); } catch (ArgumentException) { continue; } } } return(parent); }
public bool CheckType(string name) => name == type.strtype ? true : false; //thanks C# for one-string functions)) public static Value GetSmallValue(string val, Value parent = null) { //Console.WriteLine("Value is " + val); Value value = VoidValue; val = val.Trim(); if (val.StartsWith("<") && val.EndsWith(">")) //example of syntax - _loads : <wolSystem:string> ; { if (parent != null) { VirtualMachine.ThrowVMException("Default value cannot have parent value", VirtualMachine.position, ExceptionType.ValueException); } val = val.Remove(0, 1).Remove(val.Length - 2); //remove '<' and '>' string[] vals = val.Split(':'); if (vals.Length == 2) { string type_word = vals[1].Trim(), val_word = vals[0].Trim(); if (type_word == "double") { wolDouble type = new wolDouble(); type.ParseDouble(val_word); value = new Value(type); } else if (type_word == "int") { wolInt type = new wolInt(); type.ParseInt(val_word); value = new Value(type); } else if (type_word == "string") { wolString type = new wolString(); type.value = Regex.Unescape(vals[0]); value = new Value(type); } else if (type_word == "long") { wolLong type = new wolLong(); type.ParseLong(val_word); value = new Value(type); } else if (type_word == "bool") { wolBool type = new wolBool(); type.ParseBool(val_word); value = new Value(type); } else if (type_word == "void") { value = VoidValue; } else if (type_word == "short") { wolShort type = new wolShort(); type.ParseShort(val_word); value = new Value(type); } else if (type_word == "float") { wolFloat type = new wolFloat(); type.ParseFloat(val_word); value = new Value(type); } else if (type_word == "byte") { wolByte type = new wolByte(); type.ParseByte(val_word); value = new Value(type); } else if (type_word == "char") { wolChar type = new wolChar(); type.ParseChar(val_word); value = new Value(type); } else { value = new Value(VirtualMachine.GetWolClass(type_word)); foreach (string f in vals[0].Split(',')) { string[] fs = f.Split('='); try { value.type.fields[fs[0].Trim()] = GetValue(fs[1]); } catch (KeyNotFoundException) { VirtualMachine.ThrowVMException($"Field by name {fs[0]} not found", VirtualMachine.position, ExceptionType.NotFoundException); } } } return(value); } else { VirtualMachine.ThrowVMException("Value and his type not found in this string", VirtualMachine.position, ExceptionType.BLDSyntaxException); return(null); } } else if (val.StartsWith("@")) //example of syntax - plus : @a, @b ; { //Console.WriteLine(val); val = val.Remove(0, 1); //skip '@' if (parent != null) { if (parent.CheckType("Type")) { return(((wolType)parent.type).value.GetStaticField(val)); } else { return(parent.GetField(val)); } } else { try { value = VirtualMachine.mainstack.values[val]; } catch (KeyNotFoundException) { VirtualMachine.ThrowVMException($"Variable by name '{val}' not found in main stack", VirtualMachine.position, ExceptionType.NotFoundException); } return(value); } } else if (val.StartsWith("&")) //example of syntax - set : &@this, <null:void> ; { val = val.Remove(0, 1); //remove '&' if (parent != null) { VirtualMachine.ThrowVMException("Link cannot haven`t ParentValue. He can have only valid address", VirtualMachine.position, ExceptionType.ValueException); return(new Value(new wolLink())); } else { return(new Value(new wolLink(val))); } } else if (val.StartsWith("#")) //example of syntax - set : &@this, #sum ; { val = val.Remove(0, 1); //remove '#' if (parent != null) { if (parent.CheckType("Type")) { return(new Value(new wolFunc(((wolType)parent.type).value.GetStaticMethod(val)))); //one string or how make code unreadable } else { if (val == "set") { return(new Value(new wolFunc(parent.setter))); } else if (val == "get") { return(new Value(new wolFunc(parent.getter))); } else { return(new Value(new wolFunc(parent.GetMethod(val)))); //return not static method of ParentValue by name } } } else { return(VirtualMachine.FindFunc(val)); //one string) } } else if (val.StartsWith("$")) //example of syntax - equals : $void, (typeof : <null:void>) ; { if (parent != null) { VirtualMachine.ThrowVMException("Class (Type) cannot have parent value", VirtualMachine.position, ExceptionType.ValueException); } return(new Value(new wolType(val.Remove(0, 1)))); //let`s write in one string!!! } else if (val.StartsWith("%")) //example of syntax - if : ( equals : $void, (typeof : <null:void>) ), %if_block1 ; { if (parent != null) { VirtualMachine.ThrowVMException("Block cannot have parent value", VirtualMachine.position, ExceptionType.ValueException); } return(VirtualMachine.FindBlock(val.Remove(0, 1))); //one string again! } else if (val.StartsWith("(")) //example of syntax - return (typeof : @this ) ; { if (parent != null) { VirtualMachine.ThrowVMException("Expression cannot have parent value", VirtualMachine.position - val.Length, ExceptionType.ValueException); } StringBuilder buffer = new StringBuilder(); char current = val[1]; //skip '(' int pos = 1; //skip '(' byte priority = 0; while (true) //add body of expression { if (current == '(') { priority++; } else if (current == ')') { if (priority == 0) { break; } else { priority--; } } try { buffer.Append(current); current = val[++pos]; } catch (IndexOutOfRangeException) { VirtualMachine.ThrowVMException($"End of string expression ('{val}') not found", VirtualMachine.position - val.Length + pos, ExceptionType.BLDSyntaxException); } } return(Script.ParseExpression(buffer.ToString())); } else { VirtualMachine.ThrowVMException($"Value {val} cannot find", VirtualMachine.position - val.Length, ExceptionType.BLDSyntaxException); return(null); } }
public void SetValue(Value value) { string[] small_vals = Address.Split('?'); //get parts of address Value parent = null; //it`s parent value for (int i = 0; i < small_vals.Length; i++) { bool end = false; string val = small_vals[i]; if (i == small_vals.Length - 1) { end = true; } if (val.StartsWith("@")) //example of syntax - plus : @a, @b ; { val = val.Remove(0, 1); if (parent != null) { if (parent.CheckType("Type")) { if (!end) { parent = ((wolType)parent.type).value.GetStaticField(val); } else { ((wolType)parent.type).value.static_fields[val] = value; } } else { if (!end) { parent = parent.GetField(val); } else { parent.type.fields[val] = value; } } } else { try { if (!end) { parent = VirtualMachine.mainstack.values[val]; } else { VirtualMachine.mainstack.values[val] = value; } } catch (KeyNotFoundException) { VirtualMachine.ThrowVMException($"Variable by name '{val}' not found in main stack", VirtualMachine.position, ExceptionType.NotFoundException); } } } else if (val.StartsWith("&")) //example of syntax - set : &this, <null:void> ; { val = val.Remove(0, 1); if (parent != null) { if (parent.CheckType("Type")) { if (!end) { parent = ((wolType)parent.type).value.GetStaticField(val); } else { ((wolType)parent.type).value.static_fields[val] = value; } } else { if (!end) { parent = parent.GetField(val); } else { parent.type.fields[val] = value; } } } else { if (!end) { parent = VirtualMachine.mainstack.values[val]; } else { VirtualMachine.mainstack.values[val] = value; } } } else if (val.StartsWith("#")) //example of syntax - set : &this, #sum ; { val = val.Remove(0, 1); //remove '#' if (parent != null) { if (parent.CheckType("Type")) { ((wolType)parent.type).value.static_fields[val] = value; } else { parent.type.methods[val] = ((wolFunc)value.type).value; //return not static method of ParentValue by name } } else { VirtualMachine.mainstack.functions[val] = ((wolFunc)value.type).value; } } else if (val.StartsWith("$")) //example of syntax - equals : $void, (typeof : <null:void>) ; { if (parent != null) { VirtualMachine.ThrowVMException("Class (Type) cannot have parent value", VirtualMachine.position, ExceptionType.ValueException); } if (!end) { parent = new Value(new wolType(val.Remove(0, 1))); } else { VirtualMachine.mainstack.classes[val.Remove(0, 1)] = ((wolType)value.type).value; } } else if (val.StartsWith("%")) //example of syntax - if : ( equals : $void, (typeof : <null:void>) ), %if_block1 ; { if (parent != null) { VirtualMachine.ThrowVMException("Block cannot have parent value", VirtualMachine.position, ExceptionType.ValueException); } val = val.Remove(0, 1); if (!end) { parent = VirtualMachine.FindBlock(val); } else { if (!VirtualMachine.mainstack.values[val].CheckType("Block")) { VirtualMachine.ThrowVMException($"Variable by name {val} not found", VirtualMachine.position, ExceptionType.NotFoundException); } else { VirtualMachine.mainstack.values[val] = value; } } } else { VirtualMachine.ThrowVMException("Invalid syntax of linked value", VirtualMachine.position, ExceptionType.BLDSyntaxException); } } }
public static Value ParseExpression(string string_expression, Dictionary <string, Value> arguments) { //Console.WriteLine("String expression: " + string_expression); VirtualMachine.mainstack = VirtualMachine.mainstack + arguments; string[] tokens = string_expression.Split(new char[4] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); string keyword = tokens[0]; if (keyword.StartsWith("@") || keyword.StartsWith("#") || keyword.StartsWith("$") || keyword.StartsWith("%") || keyword.StartsWith("<") || keyword.StartsWith("&")) { wolFunc value = new wolFunc(); //create empty 'Func' instance for not throwing NullRefrenceException try { value = (wolFunc)Value.GetValue(keyword).type; } catch (InvalidCastException) { VirtualMachine.ThrowVMException($"'{keyword}' haven`t type Func", VirtualMachine.position, ExceptionType.InvalidTypeException); } string args = string_expression.Substring(string_expression.IndexOf(':') + 1).Trim(); //code after name of function (string with arguments) List <string> argums = new List <string>(); //array with arguments of expression int pos = 0; char current = args[0]; StringBuilder buffer = new StringBuilder(); while (true) { if (current == '(') { while (current != ')') { try { buffer.Append(current); current = args[++pos]; } catch (IndexOutOfRangeException) { VirtualMachine.ThrowVMException("End of expression not found", VirtualMachine.position - args.Length + pos, ExceptionType.BLDSyntaxException); } } } else if (current == '<') { while (current != '>') { try { buffer.Append(current); current = args[++pos]; } catch (IndexOutOfRangeException) { VirtualMachine.ThrowVMException("End of value not found", VirtualMachine.position - args.Length + pos, ExceptionType.BLDSyntaxException); } } } else if (current == ',') { argums.Add(buffer.ToString()); buffer.Clear(); try { current = args[++pos]; } catch (IndexOutOfRangeException) { break; } } else { try { buffer.Append(current); current = args[++pos]; } catch (IndexOutOfRangeException) { argums.Add(buffer.ToString()); break; } } } Value[] values = new Value[argums.Count]; //array with arguments who converted to Value for (int i = 0; i < argums.Count; i++) { values[i] = Value.GetValue(argums[i].TrimStart()); //convert string arguments to Value arguments } VirtualMachine.mainstack = VirtualMachine.mainstack - arguments; return(value.Call(values)); } else if (keyword.StartsWith("^")) { string[] toks = keyword.Remove(0, 1).Split('.'); string args = string_expression.Substring(string_expression.IndexOf(':') + 1).Trim(); //code after constructor (string with arguments) string[] argums = args.Split(','); //array with arguments of constructor Value[] values = new Value[argums.Length]; //array with arguments who converted to Value for (int i = 0; i < argums.Length; i++) { values[i] = Value.GetValue(argums[i].TrimStart()); //convert string arguments to Value arguments } VirtualMachine.mainstack = VirtualMachine.mainstack - arguments; return(new Value(VirtualMachine.GetWolClass(toks[0]), toks[1], values)); } else if (keyword.StartsWith("~")) { string args = string_expression.Substring(string_expression.IndexOf(':') + 1).Trim(); //code after destructor (string with arguments) string[] argums = args.Split(','); //array with arguments of destructor Value[] values = new Value[argums.Length]; //array with arguments who converted to Value for (int i = 0; i < argums.Length; i++) { values[i] = Value.GetValue(argums[i].TrimStart()); //convert string arguments to Value arguments } string[] toks = keyword.Remove(0, 1).Split(':'); VirtualMachine.GetWolClass(toks[0]).CallDestructor(int.Parse(toks[1]), values); VirtualMachine.mainstack = VirtualMachine.mainstack - arguments; return(Value.VoidValue); } else { bool haveExpression = true; //check on found expression by this name foreach (KeyValuePair <string, VMExpression> expression in VirtualMachine.expressions) { if (expression.Key == keyword) { List <string> argums = new List <string>(); if (string_expression.Contains(":")) { string args = string_expression.Substring(string_expression.IndexOf(':') + 1).Trim(); //code after name of expression (string with arguments) StringBuilder buffer = new StringBuilder(); byte expr = 0; //priority of expressions for (int i = 0; i < args.Length; i++) { char cur = args[i]; if (cur == ',' && expr == 0) { argums.Add(buffer.ToString()); buffer.Clear(); } else if (cur == ')' && expr > 0) { buffer.Append(cur); expr--; //Console.WriteLine("Priority ): " + expr); } else if (cur == '(' && expr >= 0) { buffer.Append(cur); expr++; //Console.WriteLine("Priority (: " + expr); } else { buffer.Append(cur); } } argums.Add(buffer.ToString()); //Console.WriteLine(string.Join(' ', argums) + '\t' + argums.Count); } else { haveExpression = true; return(expression.Value.ParseExpression()); } Value[] values = new Value[argums.Count]; //array with arguments who converted to Value for (int i = 0; i < argums.Count; i++) { values[i] = Value.GetValue(argums[i].TrimStart()); //convert string arguments to Value arguments } haveExpression = true; VirtualMachine.mainstack = VirtualMachine.mainstack - arguments; return(expression.Value.ParseExpression(values)); } else { haveExpression = false; } } if (!haveExpression) { VirtualMachine.ThrowVMException($"VM Expression by name {keyword} not found and will cannot parse", VirtualMachine.position, ExceptionType.NotFoundException); } VirtualMachine.mainstack = VirtualMachine.mainstack - arguments; return(null); } }