To extend the Frost language with new keywords, you'll need to modify several components of the compiler. Here's a step-by-step guide using the provided code as a reference:
1. Update the Parser
In the Ast.Parser.Expr module, add a new parsing function for your keyword:
parseNewKeyword :: PU.ParserAT.ExprparseNewKeyword = do srcLoc <- PU.parseSrcLoc _ <- PU.symbol "newkeyword" -- Add parsing logic for the new keyword -- For example: expr <- parseExpr return $ AT.NewKeyword srcLoc expr
Then, add this new parser to the parseTerm function:
In the Ast.Types module, add a new constructor to the Expr data type:
3. Implement Semantic Analysis
If your new keyword requires semantic analysis, update the relevant functions in the semantic analysis phase. This might involve modifying the type checker or other analysis passes.
4. Add Code Generation
Implement code generation for your new keyword in the appropriate module within Codegen.ExprGen:
5. Update Error Handling
In Codegen.Errors, add any new error types related to your keyword:
6. Modify Utility Functions
Update utility functions like getLoc in Shared.Utils to handle your new expression type:
7. Add Tests
Create new test cases in the test/ directory to ensure your keyword works as expected:
8. Update Documentation
Don't forget to update the language documentation to include information about your new keyword, its syntax, and usage examples.
By following these steps and integrating your changes with the existing codebase, you can successfully extend Frost with new keywords while maintaining its integrity and consistency.
-- | Generate LLVM IR for a new keyword expression.
generateNewKeyword :: AT.Expr -> AT.Expr -> Codegen AST.Operand
generateNewKeyword loc expr = do
-- Generate LLVM IR for your new keyword
-- For example:
exprOp <- generateExpr expr
-- Add LLVM instructions as needed
return exprOp
getLoc :: AT.Expr -> AT.SrcLoc
getLoc expr = case expr of
-- ... existing cases ...
AT.NewKeyword loc _ -> loc
testNewKeyword :: Test
testNewKeyword = TestCase $ do
let code = "newkeyword someExpression"
result <- runParser parseExpr code
assertEqual "New keyword parsing" expectedAST result