Adding New Keywords
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.Parser AT.Expr
parseNewKeyword = do
srcLoc <- PU.parseSrcLoc
_ <- PU.symbol "newkeyword"
-- Add parsing logic for the new keyword
-- For example:
expr <- parseExpr
return $ AT.NewKeyword srcLoc exprThen, add this new parser to the parseTerm function:
parseTerm :: PU.Parser AT.Expr
parseTerm =
M.choice
[ -- ... existing choices ...
, parseNewKeyword
, parseParenExpr
]2. Extend the AST
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.
Last updated