public SymArray(SymIntConst minIndex, SymIntConst maxIndex, SymType type) { MinIndex = minIndex; MaxIndex = maxIndex; Type = type; BSize = (int)Size * Type.BSize; }
protected SymVarOrConst(string name, SymType type, SymLocTypeEnum locType, bool isConst) { Name = name; Type = type; IsConst = isConst; LocType = locType; }
private static T BuildException <T>(SymType type, Token token) where T : ParserException { try { return((T)Activator.CreateInstance(typeof(T), type, token.Lexeme, token.Line, token.Column)); } catch (TargetInvocationException e) { throw e.InnerException; } }
public SymVar AddVariable(IdentifierToken variableToken, SymType type, SymVar.SymLocTypeEnum varMod, SymConst value = null) { RequireSymbolRewritable(variableToken); //type check for initial value must be performed earlier. i.e. in parser. var symVar = new SymVar(variableToken.Value, type, value, varMod); _stack.Peek().Add(symVar); return(symVar); }
private bool IsTypesEqual(SymType target, SymType source) { var lhs = target; var rhs = source; while (true) { switch (lhs) { case SymArray lArr: switch (rhs) { case SymArray rArr: if (lArr.MinIndex.Value != rArr.MinIndex.Value || lArr.MaxIndex.Value != rArr.MaxIndex.Value) { return(false); } if (lArr.Type is SymScalar && rArr.Type is SymScalar) { return(lArr.Type.GetType() == rArr.Type.GetType()); } lhs = lArr.Type; rhs = rArr.Type; continue; } break; case SymScalar lScalar: switch (rhs) { case SymScalar rScalar: return(lScalar.GetType() == rScalar.GetType()); } break; case SymRecord lRecord: switch (rhs) { case SymRecord rRecord: return(lRecord.Name == rRecord.Name); } break; default: return(lhs.GetType() == rhs.GetType()); } return(false); } }
public void RequireCast(SymType targetType, ref ExprNode source) { if (TryCast(targetType, ref source)) { return; } var token = ExprNode.GetClosestToken(source); throw new IncompatibleTypesException(targetType, source.Type, token.Lexeme, token.Line, token.Column); }
public SymFunc(string name, List <SymVar> parameters, SymTable localVariables, StatementNode body, SymType returnType) { Name = name; Parameters = parameters; LocalVariables = localVariables; Body = body; if (returnType == null) { ReturnType = new SymVoid(); } else { ReturnType = returnType; } //predefined if (localVariables == null) { return; } foreach (var localSymbol in LocalVariables.Reverse()) { var lvar = localSymbol as SymVar; Debug.Assert(lvar != null); if (lvar.LocType != SymVar.SymLocTypeEnum.Local) { continue; } LocalVariableBsize += lvar.Type.BSize; LocalVarOffsetTable.Add(lvar.Name, LocalVariableBsize); } // align LocalVariableBsize += LocalVariableBsize % 8 > 0 ? 8 - LocalVariableBsize % 8 : 0; var paramOffset = 16; foreach (var param in parameters) { ParamsOffsetTable.Add(param.Name, paramOffset); var paramSize = param.Type.BSize; // parameters will be aligned on stack paramSize += paramSize % 8 > 0 ? 8 - paramSize % 8 : 0; paramOffset += paramSize; } ParamsSizeB = paramOffset; }
public void AddFunction(IdentifierToken nameToken, List <SymVar> paramList, SymTable localVars, StatementNode body, SymType returnType) { //todo: add checks RequireSymbolRewritable(nameToken); // SymType returnType = null; // if (returnTypeToken != null) { // returnType = FindType(returnTypeToken.Value); // if (returnType == null) // throw new TypeNotFoundException(returnTypeToken.Lexeme, returnTypeToken.Line, returnTypeToken.Column); // } // _stack.Peek().Add(new SymFunc(nameToken.Value, paramList, localVars, body, returnType)); AddConst(nameToken, new SymFuncConst(nameToken.Value, new SymFunc(nameToken.Value, paramList, localVars, body, returnType), SymVarOrConst.SymLocTypeEnum.Global)); }
public void AddAliasType(IdentifierToken aliasToken, SymType aliasType) { RequireSymbolRewritable(aliasToken); // compute underlying type before adding var realType = aliasType; while (realType is SymTypeAlias typeAlias) { if (typeAlias.Type is SymArray arr) { realType = arr; break; } realType = FindType(typeAlias.Type.Name); } _stack.Peek().Add(new SymTypeAlias(aliasToken.Value, realType)); }
public OpenArray(SymType symInnerType) { InnerType = symInnerType; }
public bool CanCast(SymType targetType, ExprNode source) { return(TryCast(targetType, ref source, false)); }
public SymStringConst(string name, SymType type, string value, SymLocTypeEnum locType) : base(name, type, locType) { Value = value; InitialStringValue = value; }
public SymBoolConst(string name, SymType type, bool value, SymLocTypeEnum locType) : base(name, type, locType) { Value = value; InitialStringValue = value.ToString(); }
public SymTypeAlias(string name, SymType type) { Name = name; Type = type; }
public ArrayExpectedException(SymType type, string lexeme, int line, int column) : base(lexeme, line, column) { _type = type; }
public void AddAlias(IdentifierToken aliasToken, SymType type) { RequireSymbolRewritable(aliasToken); _stack.Peek().Add(new SymAlias(aliasToken.Value, type)); }
public void AddType(SymType symType, Token token) { //todo: check? _stack.Peek().Add(symType); }
private void AddType(SymType symType) { //todo: check? _stack.Peek().Add(symType); }
public FunctionExpectedException(SymType got, string lexeme, int line, int column) : base(lexeme, line, column) { _gotType = got; }
protected PredefinedSymFunc(string name, List <SymVar> parameters, SymTable localVariables, StatementNode body, SymType returnType) : base(name, parameters, localVariables, body, returnType) { }
public IncompatibleTypesException(SymType leftType, SymType rightType, string lexeme, int line, int column) : base(lexeme, line, column) { _leftType = leftType; _rightType = rightType; }
public CastNode(SymType type, ExprNode expr) { CastTo = type; Expr = expr; }
// 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 RecordExpectedException(SymType gotType, string lexeme, int line, int column) : base(lexeme, line, column) { _gotType = gotType; }
public SymVar(string name, SymType type, SymConst initialValue, SymLocTypeEnum locType) : base(name, type, locType, false) { InitialValue = initialValue; InitialStringValue = initialValue?.InitialStringValue; }
public WritelnUnsupportedType(SymType type, string lexeme, int line, int column) : base(lexeme, line, column) { _type = type; }
protected SymConst(string name, SymType type, SymLocTypeEnum locType) : base(name, type, locType, true) { }
public OperatorNotOverloaded(SymType type, string lexeme, int line, int column) : base(lexeme, line, column) { Message = $"Operator {Lexeme} is not overloaded for \"{type.Name}\" at {Line},{Column}."; }
public SymDoubleConst(string name, SymType type, double value, SymLocTypeEnum locType) : base(name, type, locType) { Value = value; InitialStringValue = value.ToString(); }