static bool AssignValue(string VariableName, Node Expression) { SymbolValue Result = SymbolTable.Find(sv => sv.Name == VariableName && CurrentScope.Contains(sv.Scope)); if (Result == null) { MessageBox.Show("Variable " + VariableName + " Doesn't Exist"); return(false); } else { if (Result.DataType == Expression.datatype) { Result.Value = Expression.value; } else if (Result.DataType == Token_Class.Float.ToString() && Expression.datatype == Token_Class.Integer.ToString()) { Result.Value = Expression.value; Expression.datatype = Token_Class.Float.ToString(); } else if (Result.DataType == Token_Class.Integer.ToString() && Expression.datatype == Token_Class.Float.ToString()) { Result.Value = Expression.value; Result.DataType = Token_Class.Float.ToString(); } else { //Don't forget..int goes in float !!!! MessageBox.Show("DataTypes" + Result.DataType + "and" + Expression.datatype + "Missmatch"); return(false); } } return(true); }
public static bool EvaluateCondition(SymbolValue Right, string Opp, Node Left) { bool Result = false; if (Right.DataType != Left.datatype) { MessageBox.Show("Cannot Compare different datatypes"); } else { if (Opp == "=") { if (Left.datatype == Token_Class.Integer.ToString()) { Result = (Convert.ToInt32(Right.Value) == Convert.ToInt32(Left.value)); } else if (Left.datatype == Token_Class.Float.ToString()) { Result = (Convert.ToDecimal(Right.Value) == Convert.ToDecimal(Left.value)); } } else if (Opp == ">") { if (Left.datatype == Token_Class.Integer.ToString()) { Result = (Convert.ToInt32(Right.Value) > Convert.ToInt32(Left.value)); } else if (Left.datatype == Token_Class.Float.ToString()) { Result = (Convert.ToDecimal(Right.Value) > Convert.ToDecimal(Left.value)); } } else if (Opp == "<") { if (Left.datatype == Token_Class.Integer.ToString()) { Result = (Convert.ToInt32(Right.Value) < Convert.ToInt32(Left.value)); } else if (Left.datatype == Token_Class.Float.ToString()) { Result = (Convert.ToDecimal(Right.Value) < Convert.ToDecimal(Left.value)); } } else if (Opp == "<>") { if (Left.datatype == Token_Class.Integer.ToString()) { Result = (Convert.ToInt32(Right.Value) != Convert.ToInt32(Left.value)); } else if (Left.datatype == Token_Class.Float.ToString()) { Result = (Convert.ToDecimal(Right.Value) != Convert.ToDecimal(Left.value)); } } } return(Result); }
public static void HandleDeclerationStatment(Node root) { SymbolValue sv = new SymbolValue(); HandleDatatype(root.children[0]); sv.DataType = root.children[0].datatype; root.children[1].datatype = root.children[0].datatype; HandleListIdentifier(root.children[1]); }
static bool CheckIfDeclared(string VariableName, string ParameterType) { SymbolValue Result = SymbolTable.Find(sv => sv.Name == VariableName && CurrentScope.Contains(sv.Scope)); if (Result == null || Result.DataType != ParameterType) { MessageBox.Show("Variable" + VariableName + " is not declared"); return(false); } return(true); }
public static void HandleReadStatement(Node root) { //root.children[0] read; string currentVariable = root.children[1].Name; SymbolValue Result = SymbolTable.Find(sv => sv.Name == currentVariable && CurrentScope.Contains(sv.Scope)); if (Result == null) { MessageBox.Show("Variable " + currentVariable + " doesn't exist"); } else { root.value = Result.Value; root.datatype = Result.DataType; root.children[1].value = Result.Value; root.children[1].datatype = Result.DataType; } }
public static void HandleZ(Node root) { root.children[0].datatype = root.datatype; if (root.children[0].Name == "AssignmentStatement") { HandleExpression(root.children[0].children[2]); SymbolValue sv = new SymbolValue(); sv.Name = root.children[0].children[0].Name; sv.DataType = root.children[0].datatype; sv.Scope = CurrentScope; sv.Value = "0";//mo2ktan AddVariable(sv); HandleAssignmentStatement(root.children[0]); } else { HandleParameters(root.children[0]); } }
public static void HandleParameters(Node root) { if (root.children.Count == 0) { return; } if (root.children.Count == 1) { SymbolValue sv = new SymbolValue(); if (root.datatype == Token_Class.Integer.ToString()) { root.children[0].value = 0; } else if (root.datatype == Token_Class.Float.ToString()) { root.children[0].value = 0.0; } else { root.children[0].value = "empty"; } sv.Name = root.children[0].Name; sv.Value = root.children[0].value; sv.DataType = root.datatype; sv.Scope = CurrentScope; AddVariable(sv); } else { int start = 0; if (root.children[0].Name == ",") { start = 1; } for (int i = start; i < root.children.Count; i++) { root.children[i].datatype = root.datatype; HandleParameters(root.children[i]); } } }
static bool AddVariable(SymbolValue NewSymbolVal) { SymbolValue Result = SymbolTable.Find(sv => sv.Name == NewSymbolVal.Name && sv.Scope == NewSymbolVal.Scope); if (Result != null) { if (Result.Scope == NewSymbolVal.Scope) { MessageBox.Show("Variable " + Result.Name + " already declared"); return(false); } else { SymbolTable.Add(NewSymbolVal); } } else { SymbolTable.Add(NewSymbolVal); } return(true); }
public static void HandleTerm(Node root) { if (root.children[0].Name == "Constant") { root.children[0].value = root.children[0].children[0].Name; if (root.children[0].children[0].Name.Contains(".")) { root.children[0].datatype = root.children[0].datatype.ToString(); root.children[0].datatype = Token_Class.Float.ToString(); root.children[0].children[0].datatype = Token_Class.Float.ToString(); } else { root.children[0].datatype = root.children[0].datatype.ToString(); root.children[0].datatype = Token_Class.Integer.ToString(); root.children[0].children[0].datatype = Token_Class.Integer.ToString(); } } else if (root.children[0].Name == "FunctionCall") { HandleFuncCall(root.children[0]); root.children[0].value = float.MinValue; //default value! Needs If conditions } else { SymbolValue Result = SymbolTable.Find(sv => sv.Name == root.children[0].Name && CurrentScope.Contains(sv.Scope)); if (Result == null) { MessageBox.Show("Variable " + root.children[0].Name + " doesn't exist in " + CurrentScope); } else { root.children[0].datatype = Result.DataType; root.children[0].value = Result.Value; } } root.datatype = root.children[0].datatype; root.value = root.children[0].value; }
public static void HandleListParameters(Node root, List <string> list) { if (root.children.Count == 0) { return; } if (root.children.Count == 3) { HandleDatatype(root.children[0]); list.Add(root.children[0].datatype); //root.children[1] ParameterName! root.children[1].datatype = root.children[0].datatype; SymbolValue sv = new SymbolValue(); sv.Name = root.children[1].Name; sv.Scope = CurrentScope; sv.DataType = root.children[0].datatype; sv.Value = 0;//mo2ktan AddVariable(sv); //ha3ml 7aga b namae el parameter ??? HandleListParameters(root.children[2], list); } else { //root.children[0] ->"." HandleDatatype(root.children[1]); list.Add(root.children[1].datatype); //root.children[2] ParameterName! root.children[2].datatype = root.children[1].datatype; SymbolValue sv = new SymbolValue(); sv.Name = root.children[2].Name; sv.Scope = CurrentScope; sv.DataType = root.children[1].datatype; sv.Value = 0;//mo2ktan AddVariable(sv); //ha3ml 7aga b namae el parameter ??? HandleListParameters(root.children[3], list); } }
public static void HandleConditionStatment(Node root) { bool final; if (root.children.Count == 0) { return; } if (root.Name == "ConditionStatementOR") { //ConditionOperator root.children[0]; HandleConditionStatment(root.children[1]); HandleConditionStatment(root.children[2]); root.value = root.children[1].value; } else if (root.Name == "ConditionStatementAnd") { //opp //condtion string VariableName = root.children[1].children[0].Name; SymbolValue Result = SymbolTable.Find(sv => sv.Name == VariableName && CurrentScope.Contains(sv.Scope)); if (Result == null) { MessageBox.Show("Variable " + VariableName + " doesn't exist in " + CurrentScope); } else { root.children[1].children[0].value = Result.Value; root.children[1].children[0].datatype = Result.DataType; string Opp = root.children[1].children[1].children[0].Name; HandleTerm(root.children[1].children[2]); root.children[1].value = EvaluateCondition(Result, Opp, root.children[1].children[2]); } //CondStatments HandleConditionStatment(root.children[2]); if (root.children[2].children.Count == 0) { root.value = root.children[1].value; } else { root.value = (Convert.ToBoolean(root.children[1].value) && Convert.ToBoolean(root.children[2].value)); } } else { if (root.children[0].Name == "Condition") { //Need to check every find conditions string VariableName = root.children[0].children[0].Name; SymbolValue Result = SymbolTable.Find(sv => sv.Name == VariableName && CurrentScope.Contains(sv.Scope)); if (Result == null) { MessageBox.Show("Variable " + VariableName + " doesn't exist in " + CurrentScope); } else { root.children[0].children[0].datatype = Result.DataType; string Opp = root.children[0].children[1].children[0].Name; HandleTerm(root.children[0].children[2]); root.children[0].value = EvaluateCondition(Result, Opp, root.children[0].children[2]); root.children[0].children[0].value = Result.Value; } HandleConditionStatment(root.children[1]); if (root.children[1].children.Count == 0) { root.value = root.children[0].value; } else { root.value = (Convert.ToBoolean(root.children[0].value) && Convert.ToBoolean(root.children[1].value)); } } else { HandleConditionStatment(root.children[0]); } HandleConditionStatment(root.children[1]); } final = Convert.ToBoolean(root.children[0].value) || Convert.ToBoolean(root.children[1].value); root.value = final; }