private void GenerateCodeForAssignmentOperator(SplitExpression expression) { KeywordToken theOperator = expression.Operator as KeywordToken; if ((theOperator == null) || (!theOperator.IsModificationOperator)) { throw new CompilerMessage(ErrorCode.InternalError, "GenerateCodeForAssignmentOperator called without KeywordToken"); } if (expression.LeftHandSide.Count < 1) { throw new CompilerMessage(ErrorCode.OperatorExpectsLeftHandSide, "Expected expression before '" + expression.Operator.Name + "'"); } if ((theOperator.SymbolType != PredefinedSymbol.PlusPlus) && (theOperator.SymbolType != PredefinedSymbol.MinusMinus)) { GenerateCodeForExpression(expression.RightHandSide); } if (theOperator.SymbolType != PredefinedSymbol.SetEqual) { GenerateCodeForExpression(expression.LeftHandSide); } // TODO: Apply operator //TODO: GenerateCodeToWriteNewVariableValue }
private void ProcessKeyword(KeywordToken keywordToken) { PredefinedSymbol keyword = keywordToken.SymbolType; if (keyword == PredefinedSymbol.If) { _source.ExpectKeyword(PredefinedSymbol.OpenParenthesis); Expression condition = ReadExpression(true, PredefinedSymbol.CloseParenthesis); GenerateCodeForExpression(condition); ProcessCodeBlock(); if (_source.NextIsKeyword(PredefinedSymbol.Else)) { ProcessCodeBlock(); } } else if (keyword == PredefinedSymbol.While) { _source.ExpectKeyword(PredefinedSymbol.OpenParenthesis); Expression condition = ReadExpression(true, PredefinedSymbol.CloseParenthesis); GenerateCodeForExpression(condition); ProcessCodeBlock(); } else if (keyword == PredefinedSymbol.For) { // TODO: Handle for loops } else if (keyword == PredefinedSymbol.Break) { // TODO: Handle break } else if (keyword == PredefinedSymbol.Continue) { // TODO: Handle continue } else if (keyword == PredefinedSymbol.Return) { Expression resultToReturn = ReadExpression(true, PredefinedSymbol.Semicolon); GenerateCodeForExpression(resultToReturn); // TODO: Generate code to return and release local vars FreeAllLocalVariablesButLeaveValid(); } else { throw new CompilerMessage(ErrorCode.InvalidUseOfKeyword, "Keyword '" + keywordToken.Name + "' is not valid here"); } }
private void ProcessEnumDeclaration() { Token enumName = _source.ReadNextTokenAndThrowIfAlreadyDefined(); ScriptEnum enumDefinition = new ScriptEnum(enumName.Name); enumName.Define(TokenType.EnumType, enumDefinition); enumName.IsVariableType = true; _source.ExpectKeyword(PredefinedSymbol.OpenBrace); int nextEnumValue = 0; while (!_source.NextIsKeyword(PredefinedSymbol.CloseBrace)) { Token enumEntry = _source.ReadNextTokenAndThrowIfAlreadyDefined(); KeywordToken nextToken = _source.ReadNextAsKeyword(); if (nextToken.SymbolType == PredefinedSymbol.SetEqual) { nextEnumValue = _source.ReadNextAsConstInt(); nextToken = _source.ReadNextAsKeyword(); } enumDefinition.Values.Add(enumEntry.Name, nextEnumValue); enumEntry.Define(TokenType.Constant, nextEnumValue); nextEnumValue++; if (nextToken.SymbolType == PredefinedSymbol.CloseBrace) { break; } else if (nextToken.SymbolType != PredefinedSymbol.Comma) { throw new CompilerMessage(ErrorCode.UnexpectedToken, "Expected comma at '" + nextToken.Name + "'"); } } _source.ExpectKeyword(PredefinedSymbol.Semicolon); }
private void ProcessKeyword(KeywordToken keywordToken) { PredefinedSymbol keyword = keywordToken.SymbolType; if (keyword == PredefinedSymbol.If) { _source.ExpectKeyword(PredefinedSymbol.OpenParenthesis); Expression condition = ReadExpression(true, PredefinedSymbol.CloseParenthesis); GenerateCodeForExpression(condition); ProcessCodeBlock(); if (_source.NextIsKeyword(PredefinedSymbol.Else)) { ProcessCodeBlock(); } } else if (keyword == PredefinedSymbol.While) { _source.ExpectKeyword(PredefinedSymbol.OpenParenthesis); Expression condition = ReadExpression(true, PredefinedSymbol.CloseParenthesis); GenerateCodeForExpression(condition); ProcessCodeBlock(); } else if (keyword == PredefinedSymbol.Return) { Expression resultToReturn = ReadExpression(true, PredefinedSymbol.Semicolon); GenerateCodeForExpression(resultToReturn); // TODO: Generate code to return and release local vars FreeAllLocalVariablesButLeaveValid(); } else { throw new CompilerMessage(ErrorCode.InvalidUseOfKeyword, "Keyword '" + keywordToken.Name + "' is not valid here"); } }