fisl

fisl is scheme lox
git clone git://git.alexkarle.com.com/fisl
Log | Files | Refs | README | LICENSE

commit 4230b1824fa484286a13bb55068b63ad3656b0bd (patch)
parent 6aee3ccfda3c3ddbfde92008d6e50740c4749443
Author: Alex Karle <alex@alexkarle.com>
Date:   Wed,  5 Oct 2022 20:54:20 -0400

parser: Add basic parser types from ch. 5

This gets interesting because I'm not really trying to implement the
visitor pattern in Scheme. Setting the record printers feels awfully
like Java's toString() though

Diffstat:
Mfisl.scm | 3+++
Aparser.scm | 25+++++++++++++++++++++++++
2 files changed, 28 insertions(+), 0 deletions(-)

diff --git a/fisl.scm b/fisl.scm @@ -2,14 +2,17 @@ ;; fisl -- fisl is scheme lox (load "scanner.scm") (load "util.scm") +(load "parser.scm") (import (chicken io) (chicken base) (chicken format) scanner + parser util) (define (run code fname) + (parse) (map print (scan code fname))) (define (run-prompt) diff --git a/parser.scm b/parser.scm @@ -0,0 +1,25 @@ +(module parser (parse) + + (import scheme + (chicken base) + (chicken format)) + + (define-record binary left operator right) + (set-record-printer! binary + (lambda (x out) (fprintf out "(~S ~S ~S)" (binary-operator x) (binary-left x) (binary-right x)))) + + (define-record grouping expression) + (set-record-printer! grouping + (lambda (x out) (fprintf out "(group ~S)" (grouping-expression x)))) + + (define-record literal value) + (set-record-printer! literal + (lambda (x out) (fprintf out "~S" (literal-value x)))) + + (define-record unary operator right) + (set-record-printer! unary + (lambda (x out) (fprintf out "(~S ~S)" (unary-operator x) (unary-right x)))) + + (define (parse) + (print (make-binary (make-unary "-" (make-literal 123)) "*" (make-grouping (make-literal 42))))) +)