|
| |
| Navigation |
Synopsis Parsing a Lisp expression.
Description Give the Lisp Syntax, we can now apply it to parse textual Lisp expressions
and convert them to the runtime representation
Lval.
module demo::lang::Lisra::Parse import Prelude; import demo::lang::Lisra::Syntax; import demo::lang::Lisra::Runtime; public Lval parse(str txt) = build(parse(#LispExp, txt));First we define the actual parse function ( ): it takes a string as argument and returns an Lval.
It proceeds in two steps:
build ( -- ) is defined in cases, for the various parse tree forms.
Fortunately, we do not have to spell out the details of the parse tree, but we can use concrete
patterns instead. For instance, the argument pattern
(LispExp)`<IntegerLiteral il>`says:
LispExp in this example). This is illustrated by the list case where the parentheses appear in the concrete pattern:
(LispExp)`( <LispExp* lst> )`The right-hand sides of ( -- ) deserve some attention:
Examples
rascal>import demo::lang::Lisra::Parse; ok rascal>import demo::lang::Lisra::Runtime; ok rascal>parse("1"); Lval: Integer(1) rascal>parse("x"); Lval: Atom("x") rascal>parse("(+ 5 7)"); Lval: List([ Atom("+"), Integer(5), Integer(7) ]) |