/// <summary> /// Public Constructor /// </summary> /// <param name="context">XL Context</param> /// <param name="reader">Token Reader</param> /// <param name="valueCreator">XLExpression Value Creator</param> /// <param name="operators">Operator Collection</param> public HlExpressionParser( HlExpressionReader reader, HlExpressionValueCreator valueCreator, HlExpressionOperator[] operators) { m_OpCollection = new HlExpressionOperatorCollection(operators); ValueCreator = valueCreator; Reader = reader; CurrentToken = reader.GetNext(); }
/// <summary> /// Reads Any token or none if the token stream reached the end /// </summary> /// <param name="tokens">Token Stream</param> /// <param name="start">Start Index</param> /// <param name="result">Specified Tokens</param> /// <returns></returns> public static bool ReadAnyOrNone(List <IHlToken> tokens, int start, out IHlToken result) { if (start >= 0 && tokens.Count > start) { result = tokens[start]; return(true); } result = new EofToken(); return(false); }
/// <summary> /// Advances the reader by one position inside the source tokens /// </summary> /// <returns></returns> public IHlToken Advance() { if (m_CurrentIndex < m_Input.Length) { if (IsNewLine(m_Input[m_CurrentIndex])) { m_CurrentToken = ReadNewLine(); } else if (IsSpace(m_Input[m_CurrentIndex])) { m_CurrentIndex++; return(Advance()); } else if (IsSymbol(m_Input[m_CurrentIndex])) { m_CurrentToken = ReadSymbol(); } else if (m_Input[m_CurrentIndex] == '0' && m_Input.Length - 2 > m_CurrentIndex && m_Input[m_CurrentIndex + 1] == 'x' && IsHexNumber(m_Input[m_CurrentIndex + 2])) { m_CurrentToken = ReadHexNumber(); } else if (IsNumber(m_Input[m_CurrentIndex])) { m_CurrentToken = ReadNumber(); } else if (IsLetter(m_Input[m_CurrentIndex])) { m_CurrentToken = ReadWord(); } else { m_CurrentToken = new HlTextToken( HlTokenType.Unknown, m_Input[m_CurrentIndex].ToString(), m_CurrentIndex ); m_CurrentIndex++; } return(m_CurrentToken); } m_CurrentToken = new EofToken(); return(m_CurrentToken); }
public static void WriteInlineConstructorInvocationProlog( HlCompilation compilation, HlTypeDefinition tdef, HlFuncDefOperand fdef) { if (!NeedsConstructorInvocationProlog(tdef)) { return; } for (int i = fdef.FunctionDefinition. Arguments.Length - 1; i >= 0; i--) { IHlToken valueArgument = fdef.FunctionDefinition. Arguments[i]; compilation.EmitterResult.Emit( $"POP", $"{compilation.GetFinalName( ( valueArgument as VariableDefinitionToken ).Name.ToString() )}" ); } compilation.EmitterResult.Emit($"POP", $"{compilation.GetFinalName( "this" )}"); WriteConstructorInvocationProlog( compilation, tdef, compilation.GetFinalName("this") ); compilation.EmitterResult.Emit($"PUSH", $"{compilation.GetFinalName( "this" )}"); for (int i = 0; i < fdef.FunctionDefinition. Arguments.Length; i++) { IHlToken valueArgument = fdef.FunctionDefinition. Arguments[i]; compilation.EmitterResult.Emit( $"PUSH", $"{compilation.GetFinalName( ( valueArgument as VariableDefinitionToken ).Name.ToString() )}" ); } }
public VariableDefinitionToken( IHlToken name, IHlToken typeName, IHlToken[] modifiers, IHlToken[] subtokens, IHlToken[] initializerExpression) : this( name, typeName, modifiers, subtokens, initializerExpression, null ) { }
/// <summary> /// Public Constructor /// </summary> /// <param name="name">Variable name</param> /// <param name="typeName">Variable FunctionType Name</param> /// <param name="modifiers">Variable Modifiers</param> /// <param name="subtokens">Child Tokens</param> /// <param name="initializerExpression">Initializer Expression</param> /// <param name="size">Size of the Variable</param> public VariableDefinitionToken( IHlToken name, IHlToken typeName, IHlToken[] modifiers, IHlToken[] subtokens, IHlToken[] initializerExpression, IHlToken size) : base( HlTokenType.OpVariableDefinition, subtokens, typeName.SourceIndex ) { m_Modifiers = modifiers.ToList(); Name = name; TypeName = typeName; InitializerExpression = initializerExpression; Size = size; }
/// <summary> /// Consumes the Specified Token /// Throws an Error if a different token was found /// </summary> /// <param name="type">Expected Token FunctionType</param> public void Eat(HlTokenType type) { if (CurrentToken.Type == type) { CurrentToken = Reader.GetNext(); } else { EventManager <ErrorEvent> .SendEvent( new HlTokenReadEvent( Reader.Tokens, type, CurrentToken.Type, CurrentToken.SourceIndex ) ); } }
/// <summary> /// Parses a For Expression from the Parser /// </summary> /// <param name="parser">The Parser</param> /// <returns>Parsed Expression</returns> public static HlExpression ReadFor(HlExpressionParser parser) { IHlToken ft = parser.CurrentToken; parser.Eat(HlTokenType.OpFor); parser.Eat(HlTokenType.OpBracketOpen); HlExpression vDecl = parser.ParseExpr(); if (parser.CurrentToken.Type == HlTokenType.OpSemicolon) { parser.Eat(HlTokenType.OpSemicolon); } HlExpression condition = parser.ParseExpr(); if (parser.CurrentToken.Type == HlTokenType.OpSemicolon) { parser.Eat(HlTokenType.OpSemicolon); } HlExpression vInc = parser.ParseExpr(); parser.Eat(HlTokenType.OpBracketClose); HlExpression token = null; List <HlExpression> block; if (parser.CurrentToken.Type != HlTokenType.OpBlockToken) { block = new List <HlExpression> { parser.ParseExpr() }; } else { block = HlExpressionParser.Create(new HlExpressionReader(parser.CurrentToken.GetChildren())). Parse(). ToList(); } token = new HlForOp(vDecl, condition, vInc, block.ToArray(), ft.SourceIndex); return(token); }
public FunctionDefinitionToken( IHlToken name, IHlToken retType, IHlToken[] args, IHlToken[] mods, IHlToken[] subtokens, int start, HlTypeDefinition parent = null, HlFunctionType functionType = HlFunctionType.Function) : base( HlTokenType.OpFunctionDefinition, subtokens, start ) { FunctionType = functionType; Parent = parent; FunctionName = name; FunctionReturnType = retType; m_Modifiers = mods.ToList(); Arguments = args; Block = subtokens; }
/// <summary> /// Reads None or any of the specified tokens /// </summary> /// <param name="tokens">Token Stream</param> /// <param name="start">Start Index</param> /// <param name="type">Accepted Tokens</param> /// <param name="result">Read Token</param> /// <returns>True if any token was read.</returns> public static bool ReadNoneOrAnyOf( List <IHlToken> tokens, int start, HlTokenType[] type, out IHlToken result) { foreach (HlTokenType tokenType in type) { if (ReadOneOrNone(tokens, start, tokenType, out result)) { return(true); } if (result.Type == HlTokenType.Eof) { return(false); } } result = tokens[start]; return(false); }
/// <summary> /// Creates an implemented expression /// </summary> /// <param name="parser">XLExpressionParser</param> /// <param name="currentNode">Current Expression Node</param> /// <returns></returns> public override HlExpression Create(HlExpressionParser parser, HlExpression currentNode) { IHlToken token = parser.CurrentToken; HlTokenType tt; switch (parser.CurrentToken.Type) { case HlTokenType.OpPlus: tt = HlTokenType.OpSumAssign; break; case HlTokenType.OpMinus: tt = HlTokenType.OpDifAssign; break; case HlTokenType.OpAsterisk: tt = HlTokenType.OpProdAssign; break; case HlTokenType.OpFwdSlash: tt = HlTokenType.OpQuotAssign; break; case HlTokenType.OpPercent: tt = HlTokenType.OpRemAssign; break; case HlTokenType.OpPipe: tt = HlTokenType.OpOrAssign; break; case HlTokenType.OpAnd: tt = HlTokenType.OpAndAssign; break; case HlTokenType.OpCap: tt = HlTokenType.OpXOrAssign; break; case HlTokenType.OpGreaterThan: tt = HlTokenType.OpShiftRightAssign; parser.Eat(token.Type); break; case HlTokenType.OpLessThan: tt = HlTokenType.OpShiftLeftAssign; parser.Eat(token.Type); break; default: EventManager <ErrorEvent> .SendEvent( new HlTokenReadEvent( HlTokenType.Any, parser.CurrentToken.Type ) ); tt = HlTokenType.Unknown; break; } parser.Eat(token.Type); parser.Eat(HlTokenType.OpEquality); return(new HlBinaryOp(currentNode, tt, parser.ParseExpr(PrecedenceLevel))); }
/// <summary> /// Public Constructor /// </summary> /// <param name="context">XL Context</param> /// <param name="value">Variable Value</param> public HlVarOperand(IHlToken value, int sourceIdx) : base(sourceIdx) { Value = value; }
public void AddBaseType(IHlToken token) { m_BaseTypes.Add(token); }
/// <summary> /// Public Constructor /// </summary> /// <param name="context">The XL Context</param> /// <param name="value">The Value of this Token</param> public HlValueOperand(IHlToken value) : base(value.SourceIndex) { Value = value; }
/// <summary> /// reads one or none token of the accepted type /// </summary> /// <param name="tokens">Token Stream</param> /// <param name="start">Start index</param> /// <param name="type">Token FunctionType</param> /// <param name="result">Read Token</param> /// <returns>True if token was read.</returns> public static bool ReadOneOrNone(List <IHlToken> tokens, int start, HlTokenType type, out IHlToken result) { if (start >= 0 && tokens.Count > start) { result = tokens[start]; if (tokens[start].Type == type) { return(true); } return(false); } result = new EofToken(); return(false); }
/// <summary> /// Creates a Value based on the Current State of the Expression Parser /// </summary> /// <param name="parser">The Parser</param> /// <returns>Parsed Expression</returns> public HlExpression CreateValue(HlExpressionParser parser, uint maxPrecedence) { if (parser.CurrentToken.Type == HlTokenType.OpBang || parser.CurrentToken.Type == HlTokenType.OpTilde || parser.CurrentToken.Type == HlTokenType.OpPlus || parser.CurrentToken.Type == HlTokenType.OpMinus) { if (parser.Reader.PeekNext().Type == HlTokenType.OpPlus) { parser.Eat(HlTokenType.OpPlus); parser.Eat(HlTokenType.OpPlus); HlTokenType t = HlTokenType.OpUnaryPrefixIncrement; HlExpression token = new HlUnaryOp(parser.ParseExpr(), t); return(token); } else if (parser.Reader.PeekNext().Type == HlTokenType.OpMinus) { parser.Eat(HlTokenType.OpMinus); parser.Eat(HlTokenType.OpMinus); HlTokenType t = HlTokenType.OpUnaryPrefixDecrement; HlExpression token = new HlUnaryOp(parser.ParseExpr(), t); return(token); } else { HlTokenType t = parser.CurrentToken.Type; parser.Eat(t); HlExpression token = new HlUnaryOp(parser.ParseExpr(), t); return(token); } } if (parser.CurrentToken.Type == HlTokenType.OpAnd) { HlTokenType t = parser.CurrentToken.Type; parser.Eat(t); HlExpression token = new HlUnaryOp(parser.ParseExpr(2), HlTokenType.OpReference); return(token); } if (parser.CurrentToken.Type == HlTokenType.OpAsterisk) { HlTokenType t = parser.CurrentToken.Type; parser.Eat(t); HlExpression token = new HlUnaryOp(parser.ParseExpr(2), HlTokenType.OpDeReference); return(token); } if (parser.CurrentToken.Type == HlTokenType.OpNew && maxPrecedence >= 3) { parser.Eat(parser.CurrentToken.Type); HlExpression token = new HlUnaryOp(parser.ParseExpr(2), HlTokenType.OpNew); return(token); } if (parser.CurrentToken.Type == HlTokenType.OpReturn) { IHlToken rt = parser.CurrentToken; parser.Eat(HlTokenType.OpReturn); if (parser.CurrentToken.Type == HlTokenType.OpSemicolon) { return(new HlReturnOp(null, rt.SourceIndex)); } return(new HlReturnOp(parser.ParseExpr(), rt.SourceIndex)); } if (parser.CurrentToken.Type == HlTokenType.OpContinue) { IHlToken ct = parser.CurrentToken; parser.Eat(HlTokenType.OpContinue); return(new HlContinueOp(ct.SourceIndex)); } if (parser.CurrentToken.Type == HlTokenType.OpBreak) { IHlToken bt = parser.CurrentToken; parser.Eat(HlTokenType.OpBreak); return(new HlBreakOp(bt.SourceIndex)); } if (parser.CurrentToken.Type == HlTokenType.OpIf) { return(HlSpecialOps.ReadIf(parser)); } if (parser.CurrentToken.Type == HlTokenType.OpFor) { return(HlSpecialOps.ReadFor(parser)); } if (parser.CurrentToken.Type == HlTokenType.OpWhile) { return(HlSpecialOps.ReadWhile(parser)); } if (parser.CurrentToken.Type == HlTokenType.OpBracketOpen) { parser.Eat(HlTokenType.OpBracketOpen); HlExpression token = parser.ParseExpr(); parser.Eat(HlTokenType.OpBracketClose); return(token); } if (parser.CurrentToken.Type == HlTokenType.OpThis || parser.CurrentToken.Type == HlTokenType.OpBase) { HlExpression token = new HlVarOperand( parser.CurrentToken, parser.CurrentToken.SourceIndex ); parser.Eat(parser.CurrentToken.Type); return(token); } if (parser.CurrentToken.Type == HlTokenType.OpWord || parser.CurrentToken.Type == HlTokenType.OpNew) { IHlToken item = parser.CurrentToken; parser.Eat(parser.CurrentToken.Type); HlExpression token = new HlVarOperand(item, item.SourceIndex); return(token); } if (parser.CurrentToken.Type == HlTokenType.OpVariableDefinition) { VariableDefinitionToken vd = ( VariableDefinitionToken )parser.CurrentToken; HlVarDefOperand token; if (vd.InitializerExpression != null && vd.InitializerExpression.Length != 0) { HlExpressionParser p = HlExpressionParser.Create(new HlExpressionReader(vd.InitializerExpression.ToList())); token = new HlVarDefOperand(vd, p.Parse()); } else { token = new HlVarDefOperand(vd, new HlExpression[0]); } parser.Eat(HlTokenType.OpVariableDefinition); return(token); } if (parser.CurrentToken.Type == HlTokenType.OpFunctionDefinition) { FunctionDefinitionToken fToken = ( FunctionDefinitionToken )parser.CurrentToken; parser.Eat(HlTokenType.OpFunctionDefinition); HlExpression[] expressionBlock = null; if (fToken.Mods.All(x => x.Type != HlTokenType.OpAbstractMod)) { expressionBlock = HlExpressionParser.Create(new HlExpressionReader(fToken.Block.ToList())). Parse(); } HlExpression token = new HlFuncDefOperand( fToken, expressionBlock ); return(token); } if (parser.CurrentToken.Type == HlTokenType.OpNumber || parser.CurrentToken.Type == HlTokenType.OpDecimalNumber || parser.CurrentToken.Type == HlTokenType.OpStringLiteral || parser.CurrentToken.Type == HlTokenType.OpCharLiteral) { HlExpression token = new HlValueOperand(parser.CurrentToken); parser.Eat(parser.CurrentToken.Type); return(token); } EventManager <ErrorEvent> .SendEvent(new HlTokenReadEvent( HlTokenType.Any, parser.CurrentToken.Type )); return(null); }