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() + "'"); }
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); } } }
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); } } }
/// <summary> /// Starts processing a code block from the input stream. It is assumed /// to start with a { or be a single expression. /// </summary> private void ProcessCodeBlock() { if (_source.NextIsKeyword(PredefinedSymbol.OpenBrace)) { _state.StartNewScope(); while (!_source.NextIsKeyword(PredefinedSymbol.CloseBrace)) { ProcessNextCodeStatement(true); } FreeLocalVariables(_state.CurrentScope, true); _state.EndScope(); } else { ProcessNextCodeStatement(false); } }
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() + "'"); }
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 }
private void ProcessTokenAtTopLevel(Token thisToken) { if (thisToken is ModifierToken) { _state.NextTokenModifiers.Add((ModifierToken)thisToken); } else if (thisToken.IsVariableType) { bool wasFunction = false; do { wasFunction = ParseAndAddGlobalVariableOrFunction(thisToken); }while ((!wasFunction) && (_source.NextIsKeyword(PredefinedSymbol.Comma))); if (!wasFunction) { _source.ExpectKeyword(PredefinedSymbol.Semicolon); } _state.NextTokenModifiers.Clear(); } else if (thisToken is KeywordToken) { PredefinedSymbol keyword = ((KeywordToken)thisToken).SymbolType; if (keyword == PredefinedSymbol.StructDefinition) { _output.AddStruct(ProcessStructDeclaration()); } else if (keyword == PredefinedSymbol.Enum) { ProcessEnumDeclaration(); } else if (keyword == PredefinedSymbol.Export) { ProcessExportCommand(); } else { RecordError(ErrorCode.InvalidUseOfKeyword, "Invalid use of '" + thisToken.Name + "'"); } } else if (thisToken.Name.StartsWith(Constants.NEW_SCRIPT_MARKER)) { _scriptName = thisToken.Name.Substring(Constants.NEW_SCRIPT_MARKER.Length, thisToken.Name.Length - Constants.NEW_SCRIPT_MARKER.Length - 1); } else { RecordError(ErrorCode.UnexpectedToken, "Unexpected '" + thisToken.Name + "'"); } }