5 #include "script/parser.h"
7 static void skip_whitespaces(const char *str, size_t *cursor, size_t n)
12 while (*cursor < n && isspace(str[*cursor])) {
17 struct ParseResult create_expr_from_str(const char *str,
24 skip_whitespaces(str, cursor, n);
26 return parse_failure("EOF");
29 switch (str[*cursor]) {
32 struct ParseResult car = create_expr_from_str(str, cursor, n);
37 skip_whitespaces(str, cursor, n);
39 return parse_failure("EOF");
42 if (str[*cursor] != '.') {
43 return parse_failure("Expected .");
47 skip_whitespaces(str, cursor, n);
49 return parse_failure("EOF");
52 struct ParseResult cdr = create_expr_from_str(str, cursor, n);
57 skip_whitespaces(str, cursor, n);
59 return parse_failure("EOF");
62 if (str[*cursor] != ')') {
63 return parse_failure("Expected )");
68 return parse_success(cons_as_expr(create_cons(car.expr, cdr.expr)));
72 /* TODO(#288): create_expr_from_str does not support strings */
73 return parse_failure("Strings are not supported");
77 if (isdigit(str[*cursor])) {
78 const char *nptr = str + *cursor;
80 const double x = strtod(nptr, &endptr);
83 return parse_failure("Number expected");
86 *cursor += (size_t) (endptr - nptr);
88 return parse_success(atom_as_expr(create_atom(ATOM_NUMBER, x)));
89 } else if (isalpha(str[*cursor])) {
90 /* TODO(#289): create_expr_from_str does not support symbols */
91 return parse_failure("Symbols are not supported");
96 return parse_failure("Unexpected sequence of characters");
99 struct ParseResult parse_success(struct Expr expr)
101 struct ParseResult result = {
109 struct ParseResult parse_failure(const char *error)
111 struct ParseResult result = {