Builtin Functions
We can read in Q-Expressions but they are still useless. We need some way to manipulate them.
For this we can define some built-in operators to work on our list type. Choosing a concise set of these is important. If we implement a few fundamental operations then we can use these to define new operations without adding extra C code. There are a few ways to pick these fundamental operators but I’ve chosen a set that will allow us to do everything we need. They are defined as follows.
list | Takes one or more arguments and returns a new Q-Expression containing the arguments |
head | Takes a Q-Expression and returns a Q-Expression with only of the first element |
tail | Takes a Q-Expression and returns a Q-Expression with the first element removed |
join | Takes one or more Q-Expressions and returns a Q-Expression of them conjoined together |
eval | Takes a Q-Expression and evaluates it as if it were a S-Expression |
Like with our mathematical operators we should add these functions as possible valid symbols. Afterward we can go about trying to define their behaviour in a similar way to builtin_op
.
mpca_lang(MPCA_LANG_DEFAULT,
" \
number : /-?[0-9]+/ ; \
symbol : \"list\" | \"head\" | \"tail\" \
| \"join\" | \"eval\" | '+' | '-' | '*' | '/' ; \
sexpr : '(' <expr>* ')' ; \
qexpr : '{' <expr>* '}' ; \
expr : <number> | <symbol> | <sexpr> | <qexpr> ; \
lispy : /^/ <expr>* /$/ ; \
",
Number, Symbol, Sexpr, Qexpr, Expr, Lispy)
当前内容版权归 orangeduck 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 orangeduck .