public static Return CreateReturn(CodeElement elem, Scope scope, DefType resultType) { // return = 'return' [exp] ';'; var cmd = new Return(); CodeElement expCode = elem.Codes().FirstOrDefault(); if (expCode != null) { cmd.Expression = ExpressionBuilder.CreateExpression(expCode, scope); if (resultType == DefType.Bool && !ExpBase.IsBool(cmd.Expression)) { throw new Exception(string.Format("The expression must be a bool'. {0}", expCode.GetLineAndColumn())); } if (resultType == DefType.Int && !ExpBase.IsInt(cmd.Expression)) { throw new Exception(string.Format("The expression must be an integer'. {0}", expCode.GetLineAndColumn())); } if (resultType == DefType.Float && !ExpBase.IsNumber(cmd.Expression)) { throw new Exception(string.Format("The expression must be a number'. {0}", expCode.GetLineAndColumn())); } } else if (resultType != DefType.Void) { throw new Exception(string.Format("This function must return a value. {0}", elem.GetLineAndColumn())); } return(cmd); }
public static ValueBase Create(DefType theType, Variables runtime, ExpBase exp) { ValueBase variable; switch (theType) { case DefType.Int: variable = new ValueTyped <int>() { Value = ((ExpTyped <int>)exp).Compute(runtime) }; break; case DefType.String: variable = new ValueTyped <string>() { Value = exp.RunAsString(runtime) }; break; case DefType.Float: variable = new ValueTyped <float>() { Value = exp.RunAsFloat(runtime) }; break; case DefType.Bool: variable = new ValueTyped <bool>() { Value = ((ExpTyped <bool>)exp).Compute(runtime) }; break; case DefType.Void: throw new Exception("Can't create a variable of type void."); default: throw new Exception("Unknown variable type."); } return(variable); }
public static Assign CreateAssign(CodeElement elem, Scope scope) { // assign = identifier '=' exp ';'; var cmd = new Assign(); CodeElement idElem = elem.Codes(WordIdentifier).First(); cmd.VariableName = idElem.Value; cmd.VariableType = scope.ExistsVariable(cmd.VariableName, idElem); CodeElement expCode = elem.Codes().Last(); cmd.Expression = ExpressionBuilder.CreateExpression(expCode, scope); if (cmd.VariableType == DefType.Bool && !ExpBase.IsBool(cmd.Expression)) { throw new Exception(string.Format("The expression must be a bool'. {0}", expCode.GetLineAndColumn())); } if (cmd.VariableType == DefType.Int && !ExpBase.IsInt(cmd.Expression)) { throw new Exception(string.Format("The expression must be an integer'. {0}", expCode.GetLineAndColumn())); } if (cmd.VariableType == DefType.Float && !ExpBase.IsNumber(cmd.Expression)) { throw new Exception(string.Format("The expression must be a number'. {0}", expCode.GetLineAndColumn())); } return(cmd); }
public static VariableDef CreateVariableDef(CodeElement elem, Scope scope) { // variableDef = typeAndId '=' exp ';'; var def = CreateDeclare(elem.Codes(WordDeclare).First(), true); var cmd = new VariableDef() { Name = def.TheName, VariableType = def.TheType }; if (elem.Codes().Count() == 2) { CodeElement expCode = elem.Codes().Last(); cmd.Expression = ExpressionBuilder.CreateExpression(expCode, scope); if (cmd.VariableType == DefType.Bool && !ExpBase.IsBool(cmd.Expression)) { throw new Exception(string.Format("The expression must be a bool'. {0}", expCode.GetLineAndColumn())); } if (cmd.VariableType == DefType.Int && !ExpBase.IsInt(cmd.Expression)) { throw new Exception(string.Format("The expression must be an integer'. {0}", expCode.GetLineAndColumn())); } if (cmd.VariableType == DefType.Float && !ExpBase.IsNumber(cmd.Expression)) { throw new Exception(string.Format("The expression must be a number'. {0}", expCode.GetLineAndColumn())); } } if (scope.ExistsFunction(cmd.Name)) { throw new Exception(string.Format("A Function called '{0}', {1}, is declared", cmd.Name, elem.GetLineAndColumn())); } scope.Vars.BuildVariable(cmd.VariableType, cmd.Name, elem); return(cmd); }
public static While CreateWhile(CodeElement elem, Scope scope, DefType resultType) { // loop = 'while' '(' exp ')' body; ExpBase expbase = ExpressionBuilder.CreateExpression(elem.Codes().First(), scope); var cmd = new While(); cmd.Expression = expbase as ExpTyped <bool>; if (cmd.Expression == null) { throw new Exception(string.Format("The expression type must be a boolean, {0}", elem.GetLineAndColumn())); } cmd.Body = CreateBody(elem.Codes(WordBody).First(), scope, resultType, out bool ret); return(cmd); }
private void SetVariable(string name, ExpBase exp, Variables expVariables) { ValueBase variable; if (Vars.TryGetValue(name, out variable)) { variable.SetValue(expVariables, exp); } else if (_runtimeParent != null) { _runtimeParent.SetVariable(name, exp, expVariables); } else { throw new Exception(string.Format("A variable called '{0}' is not declared", name)); } }
public static If CreateIf(CodeElement elem, Scope scope, DefType resultType, out bool alwaysReturnValue) { // if = 'if' '(' exp ')' body ['else' body]; ExpBase expbase = ExpressionBuilder.CreateExpression(elem.Codes().First(), scope); var cmd = new If(); cmd.Expression = expbase as ExpTyped <bool>; if (cmd.Expression == null) { throw new Exception(string.Format("The expression type must be a boolean, {0}", elem.GetLineAndColumn())); } bool trueReturn; cmd.TrueStatement = CreateBody(elem.Codes(WordBody).First(), scope, resultType, out trueReturn); bool elseReturn; cmd.ElseStatement = CreateBody(elem.Codes(WordBody).Last(), scope, resultType, out elseReturn); alwaysReturnValue = trueReturn && elseReturn; return(cmd); }
public override void SetValue(Variables runtime, ExpBase exp) { if (typeof(TType) == typeof(int)) { (this as ValueTyped <int>).Value = ((ExpTyped <int>)exp).Compute(runtime); } else if (typeof(TType) == typeof(float)) { (this as ValueTyped <float>).Value = exp.RunAsFloat(runtime); } else if (typeof(TType) == typeof(string)) { (this as ValueTyped <string>).Value = exp.RunAsString(runtime); } else if (typeof(TType) == typeof(bool)) { (this as ValueTyped <bool>).Value = ((ExpTyped <bool>)exp).Compute(runtime); } else { throw new Exception("Unknown variable type."); } }
public void DeclareVariable(DefType theType, string name, ExpBase exp) { ValueBase variable = ValueBase.Create(theType, this, exp); Vars.Add(name, variable); }
public void SetVariable(string name, ExpBase exp) { SetVariable(name, exp, this); }
//public string Name; public abstract void SetValue(Variables runtime, ExpBase exp);