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 /* TODO: create_expr_from_str doesn't no support comments */
26 skip_whitespaces(str, cursor, n);
28 return parse_failure("EOF");
31 switch (str[*cursor]) {
34 struct ParseResult car = create_expr_from_str(str, cursor, n);
39 skip_whitespaces(str, cursor, n);
41 return parse_failure("EOF");
44 if (str[*cursor] != '.') {
45 return parse_failure("Expected .");
49 skip_whitespaces(str, cursor, n);
51 return parse_failure("EOF");
54 struct ParseResult cdr = create_expr_from_str(str, cursor, n);
59 skip_whitespaces(str, cursor, n);
61 return parse_failure("EOF");
64 if (str[*cursor] != ')') {
65 return parse_failure("Expected )");
70 return parse_success(cons_as_expr(create_cons(car.expr, cdr.expr)));
74 /* TODO(#288): create_expr_from_str does not support strings */
75 return parse_failure("Strings are not supported");
79 if (isdigit(str[*cursor])) {
80 const char *nptr = str + *cursor;
82 const double x = strtod(nptr, &endptr);
85 return parse_failure("Number expected");
88 *cursor += (size_t) (endptr - nptr);
90 return parse_success(atom_as_expr(create_atom(ATOM_NUMBER, x)));
91 } else if (isalpha(str[*cursor])) {
92 /* TODO(#289): create_expr_from_str does not support symbols */
93 return parse_failure("Symbols are not supported");
98 return parse_failure("Unexpected sequence of characters");
101 struct ParseResult parse_success(struct Expr expr)
103 struct ParseResult result = {
111 struct ParseResult parse_failure(const char *error)
113 struct ParseResult result = {