module demo::lang::Lisra::Parse
rascal-0.40.17
Usage
import demo::lang::Lisra::Parse;
Dependencies
import Prelude;
import demo::lang::Lisra::Syntax;
import demo::lang::Lisra::Runtime;
function parse
Lval parse(str txt) = build(parse(#LispExp, txt));
function build
Build Abstract Syntax Tree: Transform a LispExp to an Lval.
Lval build((LispExp)`<IntegerLiteral il>`) = Integer(toInt("<il>"));
Lval build((LispExp)`<AtomExp at>`) = Atom("<at>");
Lval build((LispExp)`( <LispExp* lst> )`) = List([build(l) | l <- lst]);
Tests
test build1
test bool build1() = build((LispExp) `42`) == Integer(42);
test build2
test bool build2() = build((LispExp) `abc`) == Atom("abc");
test build3
test bool build3() = build((LispExp) `(abc 42)`) == List([Atom("abc"), Integer(42)]);
test parse1
test bool parse1() = parse("123") == Integer(123);
test parse2
test bool parse2() = parse("abc") == Atom("abc");
test parse3
test bool parse3() = parse("()") == List([]);
test parse4
test bool parse4() = parse("(123)") == List([Integer(123)]);
test parse5
test bool parse5() = parse("(123 abc)") == List([Integer(123), Atom("abc")]);