public PrinterNode Visit(CastNode node) { var pNode = new PrinterNode($"({node.Type.Name})"); pNode.AddChild(node.Expr.Accept(this)); return(pNode); }
public type GetType(CastNode cnode, Environment env) { type t1 = TypeNodeToType(cnode.primitive, env); type t2 = GetType(cnode.tnode, env); bool b = t1 is NumericType && t2 is NumericType; if (!b) { throw new Exception("Only numeric types can be casted"); } return(t1); }
public bool Visit(CastNode node) { if (node.Type != null) { return(true); } node.Expr.Accept(this); if (!_typeChecker.CanCast(node.CastTo, node.Expr)) { var t = ExprNode.GetClosestToken(node.Expr); throw new IncompatibleTypesException(node.CastTo, node.Expr.Type, t.Lexeme, t.Line, t.Column); } node.Type = node.CastTo; return(true); }
// try cast source to target public bool TryCast(SymType targetType, ref ExprNode source, bool canModify = true) { var realSourceType = source.Type is SymTypeAlias sourceTypeAlias ? sourceTypeAlias.Type : source.Type; var realTargetType = targetType is SymTypeAlias targetTypeAlias ? targetTypeAlias.Type : targetType; switch (realTargetType) { // scalars // double case SymDouble _: switch (realSourceType) { case SymInt _: var t = ExprNode.GetClosestToken(source); if (!canModify) { return(true); } source = new CastNode(_stack.SymDouble, source); source.Type = _stack.SymDouble; return(true); case SymDouble _: return(true); } break; // end double // int case SymInt _: switch (realSourceType) { case SymInt _: return(true); } break; // end int // char case SymChar _: switch (realSourceType) { case SymChar _: return(true); } break; // end char // bool case SymBool _: switch (realSourceType) { case SymBool _: return(true); case SymInt _: if (!canModify) { return(true); } source = new CastNode(_stack.SymBool, source); source.Type = _stack.SymBool; return(true); } break; // end bool case SymArray target: switch (realSourceType) { case SymArray sourceType: return(IsTypesEqual(target, sourceType)); } break; default: return(IsTypesEqual(realSourceType, realTargetType)); } return(false); }
public string Print(CastNode cnode) { return("(" + Print(cnode.primitive) + ")" + " " + Print(cnode.tnode)); }