public bool next(List<Token> tokens) { if (lineRemaining == FileReader._EOF) return false; if (lineRemaining == "") { nextLine(); return true; } foreach (TokenDefenition def in defenitions) { int matched = def.matcher.match(lineRemaining); if (matched <= 0) continue; int column = position + 1; position += matched; // comment lines made if (def.tokenType == TokenType.SINGLELINE) matched = lineRemaining.Length; // stripping strings from "" string content = lineRemaining.Substring(0, matched); if (def.tokenType == TokenType.TEXT) content = content.Substring(1, content.Length - 2); token = new Token(def.tokenType, position, lineNumber, column, indentationLevel, content); lineRemaining = lineRemaining.Substring(matched); if (lineRemaining.Length == 0) { nextLine(); } return true; } Console.WriteLine("WARNING - Unidentifier token '" + lineRemaining + "' on line: " + lineNumber); tokens.Add(new Token(TokenType.NONE, token.index, token.line, token.column,token.level, lineRemaining)); nextLine(); return true; }
public static Compiler.Type ConvertTokenType(Token token) { switch (token.TypeId) { case Token.Type.Keyword: switch ((TokenizerKeyword)token.Value) { case TokenizerKeyword.Null: return(new Compiler.Type(NullTypeInfo)); case TokenizerKeyword.Void: return(new Compiler.Type(VoidTypeInfo)); case TokenizerKeyword.True: return(new Compiler.Type(BoolTypeInfo)); case TokenizerKeyword.False: return(new Compiler.Type(BoolTypeInfo)); case TokenizerKeyword.Int: return(new Compiler.Type(IntTypeInfo)); case TokenizerKeyword.Float: return(new Compiler.Type(FloatTypeInfo)); case TokenizerKeyword.Char: return(new Compiler.Type(CharTypeInfo)); case TokenizerKeyword.String: return(new Compiler.Type(StringTypeInfo)); default: throw new InvalidOperationException(); } case Token.Type.Int: return(new Compiler.Type(IntTypeInfo)); case Token.Type.Char: return(new Compiler.Type(CharTypeInfo)); case Token.Type.Float: return(new Compiler.Type(FloatTypeInfo)); case Token.Type.String: return(new Compiler.Type(StringTypeInfo)); default: throw new InvalidOperationException(); } }
public Number(Token token, double value) : base(token) { this.value = value; }
public WrongTokenFound(Token token, string rawValue) : base(CreateMessage(token, rawValue)) { }
public DivisionExpression(Token token, Expression leftHandStatement, Expression rightHandStatement) : base(token, leftHandStatement, rightHandStatement) { }
public Bool(Token token, bool value) : base(token) { this.value = value; }
public Identifier(Token token, string content) : base(token) { name = content; }
public Condition(Token token, Expression leftHandStatement, Expression rightHandStatement) : base(token, leftHandStatement, rightHandStatement) { }
public Expression(Token token) : base(token) { }
public Literal(Token token) => Value = new VariableInfo(ConvertTokenType(token), null)
private static string CreateMessage(Token token) => string.Format("(r:{0}, c:{1}) syntax error: type '{2}' not found", token.RowIndex, token.ColIndex, (string)token.Value);
public TypeNotFound(Token token) : base(CreateMessage(token)) { }
private static bool IsOperator(Token token, Operator @operator) => token.TypeId == Token.Type.Operator && (Operator)token.Value == @operator;
private static bool IsTypeName(Token token) => token.TypeId == Token.Type.Identifier && Compiler.Types.ContainsKey((string)token.Value) || token.TypeId == Token.Type.Keyword && ((Keyword)token.Value & Keyword.Type) != 0;
private static string CreateMessage(Token token, string rawValue) => string.Format("(r:{0}, c:{1}) syntax error: '{2}' expected, but {3} found", token.RowIndex, token.ColIndex, rawValue, token.RawValue);
public MultiplyExpression(Token token, Expression leftHandStatement, Expression rightHandStatement) : base(token, leftHandStatement, rightHandStatement) { }
public Call(Token token) : base(token) { }
public Text(Token token, string value) : base(token) { this.value = value; }
public BinaryExpression(Token token, Expression leftHandStatement, Expression rightHandStatement) : base(token) { this.leftHandStatement = leftHandStatement; this.rightHandStatement = rightHandStatement; }
public Assignment(Token token) : base(token) { }
public Statement(Token token) : base(token) { }
public Primitive(Token token) : base(token) { }
public Identifier(Token token) : base(token) { name = token.content; }
public Symbol(Token token) { this.token = token; }
public ProgramBlock(Token token, List<Statement> statements) { Symbol symbol = new Symbol(token); this.statements = statements; }
static Expression produceExpression(Token token, Expression leftHandStatement, Expression rightHandStatement) { TokenType type = token.tokenType; switch (type) { case TokenType.ADD: return new AdditionExpression(token, leftHandStatement, rightHandStatement); case TokenType.SUBTRACT: return new SubtractExpression(token, leftHandStatement, rightHandStatement); case TokenType.MULTIPLY: return new MultiplyExpression(token, leftHandStatement, rightHandStatement); case TokenType.DIVISION: return new DivisionExpression(token, leftHandStatement, rightHandStatement); case TokenType.MODULO: return new ModuloExpression(token, leftHandStatement, rightHandStatement); case TokenType.SMALLER: return new SmallerCondition(token, leftHandStatement, rightHandStatement); case TokenType.SMALLEREQUAL: return new SmallerEqualCondition(token, leftHandStatement, rightHandStatement); case TokenType.LARGER: return new GreaterCondition(token, leftHandStatement, rightHandStatement); case TokenType.LARGEREQUAL: return new GreaterEqualCondition(token, leftHandStatement, rightHandStatement); case TokenType.EQUAL: return new EqualCondition(token, leftHandStatement, rightHandStatement); case TokenType.NOTEQUAL: return new NotEqualCondition(token, leftHandStatement, rightHandStatement); case TokenType.AND: return new AndCondition(token, leftHandStatement, rightHandStatement); case TokenType.OR: return new OrCondition(token, leftHandStatement, rightHandStatement); } return null; }
public Block(Token token, List<Statement> statements) : base(token) { this.statements = statements; }
public SubtractExpression(Token token, Expression leftHandStatement, Expression rightHandStatement) : base(token, leftHandStatement, rightHandStatement) { }