private Expression ReadExpression(ScriptReader source, bool readTerminatingSymbolFromStream, params PredefinedSymbol[] endsWithSymbols) { Expression expression = new Expression(); int bracketLevel = 0; while (bracketLevel >= 0) { foreach (PredefinedSymbol terminatingSymbol in endsWithSymbols) { if ((bracketLevel == 0) && (source.NextIsKeyword(terminatingSymbol, !readTerminatingSymbolFromStream))) { return(expression); } } Token thisToken = source.PeekNextToken(); if (thisToken is EndOfStreamToken) { throw new CompilerMessage(ErrorCode.EndOfInputReached, "End of script reached in the middle of an expression"); } CompilerUtils.AdjustBracketLevelIfTokenIsBracket(thisToken, ref bracketLevel); if (bracketLevel >= 0) { expression.Add(source.ReadNextToken()); } } throw new CompilerMessage(ErrorCode.UnexpectedToken, "Unexpected '" + source.ReadNextToken() + "'"); }
public CompileResults CompileScript(string script) { ITokenizer tokenizer = CompilerFactory.CreateTokenizer(); _tokenizedScript = tokenizer.TokenizeScript(script); _source = new ScriptReader(_tokenizedScript); _output = new CompiledScript(); while (true) { Token thisToken = _source.ReadNextToken(); if (thisToken is EndOfStreamToken) { break; } try { ProcessTokenAtTopLevel(thisToken); } catch (CompilerMessage error) { RecordError(error.Code, error.Message); break; } } return(_results); }
public CompileResults CompileScript(string script) { ITokenizer tokenizer = CompilerFactory.CreateTokenizer(); _tokenizedScript = tokenizer.TokenizeScript(script); _source = new ScriptReader(_tokenizedScript); _output = new CompiledScript(); while (true) { Token thisToken = _source.ReadNextToken(); if (thisToken is EndOfStreamToken) { break; } try { ProcessTokenAtTopLevel(thisToken); } catch (CompilerMessage error) { RecordError(error.Code, error.Message); break; } } return _results; }
private void GenerateCodeForExpressionWithoutOperator(Expression expression) { System.Diagnostics.Trace.WriteLine(expression.ToString()); ScriptReader reader = new ScriptReader(expression, _source.LineNumber); Token firstToken = reader.ReadNextToken(); Token memberName = null; bool staticAccess = false; if (firstToken.IsVariableType) { staticAccess = true; } else if (reader.NextIsKeyword(PredefinedSymbol.OpenSquareBracket)) { Expression arrayIndex = ReadExpression(reader, true, PredefinedSymbol.CloseSquareBracket); GenerateCodeForExpression(arrayIndex); // TODO: Array access } if (reader.NextIsKeyword(PredefinedSymbol.Dot)) { if ((firstToken.Type != TokenType.LocalVariable) && (firstToken.Type != TokenType.GlobalVariable) && (firstToken.Type != TokenType.StructType)) { throw new CompilerMessage(ErrorCode.ParentIsNotAStruct, "'" + firstToken.Name + "' is not a struct"); } memberName = reader.ReadNextToken(); // TODO: struct member stuff if (staticAccess) { } } else if (staticAccess) { throw new CompilerMessage(ErrorCode.InvalidUseOfStruct, "Struct cannot be used in this way"); } else { // TODO: just read the variable itself / execute the function } // TODO: Code this }
public static void SetArrayPropertiesOnTokenFromStream(ScriptReader source, Token variableName) { if (source.NextIsKeyword(PredefinedSymbol.OpenSquareBracket)) { variableName.IsArray = true; if (source.NextIsKeyword(PredefinedSymbol.CloseSquareBracket)) { variableName.ArraySize = Token.ARRAY_SIZE_DYNAMIC; } else { variableName.ArraySize = source.ReadNextAsConstInt(); source.ExpectKeyword(PredefinedSymbol.CloseSquareBracket); } } }
private Expression ReadExpression(ScriptReader source, bool readTerminatingSymbolFromStream, params PredefinedSymbol[] endsWithSymbols) { Expression expression = new Expression(); int bracketLevel = 0; while (bracketLevel >= 0) { foreach (PredefinedSymbol terminatingSymbol in endsWithSymbols) { if ((bracketLevel == 0) && (source.NextIsKeyword(terminatingSymbol, !readTerminatingSymbolFromStream))) { return expression; } } Token thisToken = source.PeekNextToken(); if (thisToken is EndOfStreamToken) { throw new CompilerMessage(ErrorCode.EndOfInputReached, "End of script reached in the middle of an expression"); } CompilerUtils.AdjustBracketLevelIfTokenIsBracket(thisToken, ref bracketLevel); if (bracketLevel >= 0) { expression.Add(source.ReadNextToken()); } } throw new CompilerMessage(ErrorCode.UnexpectedToken, "Unexpected '" + source.ReadNextToken() + "'"); }
/// <summary> /// Starts processing a code block from the input stream. It is assumed /// to start with a { or be a single expression. /// </summary> public void Process(ScriptReader script) { _source = script; ProcessCodeBlock(); }