public static Type WhileToType(WhileType whileType) { Type type = typeof(object); switch (whileType) { case WhileType.INT: { type = typeof(int); break; } case WhileType.BOOL: { type = typeof(bool); break; } case WhileType.STRING: { type = typeof(string); break; } } return(type); }
/// <summary> /// Set a variable type /// </summary> /// <param name="name">varaible name</param> /// <param name="variableType">variable type</param> /// <returns>true if variable is a new variable in scope</returns> public bool SetVariableType(string name, WhileType variableType) { bool creation = !variables.ContainsKey(name); variables[name] = variableType; return(creation); }
public static string WhileToCSharp(WhileType whileType) { string cSharpType = ""; switch (whileType) { case WhileType.INT: { cSharpType = "int"; break; } case WhileType.BOOL: { cSharpType = "bool"; break; } case WhileType.STRING: { cSharpType = "string"; break; } } return(cSharpType); }
public WhileNode(Node test, Node body, WhileType type) : base(NodeType.While) { Test = test; Body = body; Loop = type; }
private void SemanticCheck(ReturnStatement ast, CompilerContext context) { WhileType valType = expressionTyper.TypeExpression(ast.Value, context); if (valType != WhileType.INT) { throw new TypingException($"bad return type : {valType} expecting INT"); } }
public WhileType TypeExpression(BinaryOperation operation, CompilerContext context) { operation.CompilerScope = context.CurrentScope; WhileType left = TypeExpression(operation.Left, context); operation.Left.Whiletype = left; WhileType right = TypeExpression(operation.Right, context); operation.Right.Whiletype = right; WhileType resultType = signatures.CheckBinaryOperationTyping(operation.Operator, left, right); return(resultType); }
public WhileType TypeExpression(Neg neg, CompilerContext context) { WhileType positiveVal = TypeExpression(neg.Value, context); neg.CompilerScope = context.CurrentScope; if (positiveVal != WhileType.INT) { throw new SignatureException($"invalid operation type({positiveVal}) : {neg.Dump("")}"); } else { return(WhileType.INT); } }
public WhileType GetVariableType(string name) { WhileType varType = WhileType.NONE; if (variables.ContainsKey(name)) { varType = variables[name]; } else if (ParentScope != null) { varType = ParentScope.GetVariableType(name); } return(varType); }
private void SemanticCheck(WhileStatement ast, CompilerContext context) { WhileType cond = expressionTyper.TypeExpression(ast.Condition, context); if (cond != WhileType.BOOL) { throw new SignatureException($"invalid condition type {ast.Condition.Dump("")} at {ast.Position.ToString()}"); } ast.CompilerScope = context.CurrentScope; context.OpenNewScope(); SemanticCheck(ast.BlockStmt, context); context.CloseScope(); }
private void SemanticCheck(AssignStatement ast, CompilerContext context) { WhileType valType = expressionTyper.TypeExpression(ast.Value, context); bool varExists = context.VariableExists(ast.VariableName); //ast.CompilerScope = context.CurrentScope; if (varExists) { WhileType varType = context.GetVariableType(ast.VariableName); if (varType != valType) { throw new TypingException($"bad assignment : {valType} expecting {varType}"); } } else { bool creation = context.SetVariableType(ast.VariableName, valType); ast.IsVariableCreation = creation; ast.CompilerScope = context.CurrentScope; } }
public WhileType TypeExpression(Expression expr, CompilerContext context) { if (expr is BoolConstant b) { return(TypeExpression(b, context)); } if (expr is IntegerConstant i) { return(TypeExpression(i, context)); } if (expr is StringConstant s) { return(TypeExpression(s, context)); } if (expr is BinaryOperation binary) { return(TypeExpression(binary, context)); } if (expr is Neg neg) { return(TypeExpression(neg, context)); } if (expr is Not not) { return(TypeExpression(not, context)); } if (expr is Variable variable) { WhileType varType = context.GetVariableType(variable.Name); if (varType == WhileType.NONE) { throw new TypingException($" variable {variable.Name} {variable.Position} is not intialized"); } variable.CompilerScope = context.CurrentScope; return(varType); } throw new SignatureException($"unknow expression type ({expr.GetType().Name})"); }
public WhileType CheckBinaryOperationTyping(BinaryOperator oper, WhileType left, WhileType right) { WhileType result; if (binaryOperationSignatures.ContainsKey(oper)) { List <Signature> signatures = binaryOperationSignatures[oper]; Signature res = signatures.Find((Signature sig) => sig.Match(left, right)); if (res != null) { result = res.Result; } else { throw new SignatureException($"invalid operation {left} {oper} {right}"); } } else { result = left; } return(result); }
public bool SetVariableType(string name, WhileType variableType) { return(CurrentScope.SetVariableType(name, variableType)); }
public Signature(WhileType left, WhileType right, WhileType result) { Left = left; Right = right; Result = result; }
public bool Match(WhileType l, WhileType r) { return((Left == WhileType.ANY || l == Left) && (Right == WhileType.ANY || r == Right)); }
private void SemanticCheck(PrintStatement ast, CompilerContext context) { WhileType val = expressionTyper.TypeExpression(ast.Value, context); }
public TypedValue(string val) { ValueType = WhileType.STRING; value = val; }
public TypedValue Evaluate(BinaryOperation operation, InterpreterContext context) { TypedValue left = Evaluate(operation.Left, context); TypedValue right = Evaluate(operation.Right, context); WhileType resultType = CheckBinaryOperationTyping(operation.Operator, left.ValueType, right.ValueType); object value = null; switch (operation.Operator) { case BinaryOperator.ADD: { value = left.IntValue + right.IntValue; break; } case BinaryOperator.SUB: { value = left.IntValue - right.IntValue; break; } case BinaryOperator.MULTIPLY: { value = left.IntValue * right.IntValue; break; } case BinaryOperator.DIVIDE: { value = left.IntValue / right.IntValue; break; } case BinaryOperator.AND: { value = left.BoolValue && right.BoolValue; break; } case BinaryOperator.OR: { value = left.BoolValue || right.BoolValue; break; } case BinaryOperator.GREATER: { switch (left.ValueType) { case WhileType.BOOL: { value = left.BoolValue == true && right.BoolValue == false; break; } case WhileType.INT: { value = left.IntValue > right.IntValue; break; } case WhileType.STRING: { value = left.StringValue.CompareTo(right.StringValue) == 1; break; } default: { value = false; break; } } break; } case BinaryOperator.LESSER: { switch (left.ValueType) { case WhileType.BOOL: { value = left.BoolValue == false && right.BoolValue == true; break; } case WhileType.INT: { value = left.IntValue < right.IntValue; break; } case WhileType.STRING: { value = left.StringValue.CompareTo(right.StringValue) == -1; break; } default: { value = false; break; } } break; } case BinaryOperator.EQUALS: { switch (left.ValueType) { case WhileType.BOOL: { value = left.BoolValue == right.BoolValue; break; } case WhileType.INT: { value = left.IntValue == right.IntValue; break; } case WhileType.STRING: { value = left.StringValue == right.StringValue; break; } default: { value = false; break; } } break; } case BinaryOperator.DIFFERENT: { switch (left.ValueType) { case WhileType.BOOL: { value = left.BoolValue != right.BoolValue; break; } case WhileType.INT: { value = left.IntValue != right.IntValue; break; } case WhileType.STRING: { value = left.StringValue != right.StringValue; break; } default: { value = false; break; } } break; } case BinaryOperator.CONCAT: { value = left.StringValue + right.StringValue; break; } default: { value = null; break; } } return(new TypedValue(resultType, value)); }
public TypedValue(int val) { ValueType = WhileType.INT; value = val; }
public TypedValue(bool val) { ValueType = WhileType.BOOL; value = val; }
public TypedValue(WhileType valType, object val) { ValueType = valType; Value = val; }
public WhileStat(ExpNode exp, Node stat, WhileType whileType) { Exp = exp; Stat = stat; WhileType = whileType; }