完整的 OurC 和 OurScheme 直譯器程式,已經完全通過 Project 4,僅做為紀錄之用
請勿下載程式在提交系統上測試,不管改了多少內容
Notes : 有部分 spec 內容和實際測資有出入,要自己去注意到
- Variables (int, float, char, string, bool)
- Arrays
- Functions (including lexical scope, recursion, pass params by reference)
- Environments (support for nested scopes)
- Conditional statements (if, if-else)
- Loops (while, do-while)
- Standard input/output (cin, cout)
- Formatted output (see example p4-12-1)
- Error handling
- Debug mode
- Data types (number, symbol, string, boolean, nil, error-object)
- Lists and pairs (standard lists, dotted pairs)
- Functional forms (quote, begin, and, or, cond, if)
- Functions (user-defined procedures, lambda, recursive calls, lexical scope)
- Environments (support for nested local environments using let)
- Variable binding and mutation (define, set!)
- Primitives (cons, car, cdr, eqv?, equal?, string operations, arithmetic & relational operators)
- Standard input/output (read, write, display-string, newline, type conversions)
- Verbose mode (verbose, verbose? to control define and clean-environment feedback)
- Error handling & recovery (precise line/column lexing errors, runtime checks, no-return-value tracking)
g++ p4.cpp -o p4.exe./p4.exe < inputs/p4-01.txt這兩份直譯器完成於 114-2 學期,先做好 OurC 才做好 OurScheme。 已知問題:OurC 採用 Recursive Descent Parser 並在解析語意後直接執行(未建立 AST 或產生中間碼),遞迴深度、尾遞迴優化與陣列邊界檢查未做完整處理,運算子的語意錯誤(應透過 += 實作 + 不是當前的用 + 實作 +=),迴圈內重複生成大量 const 物件佔據 heap 空間並拖慢執行效能(應改為使用 static const);後續製作的 OurScheme 完全採用 AST ,架構相對完整但也沒有做尾遞迴優化。