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", *cursor);
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", *cursor);
44 if (str[*cursor] != '.') {
45 return parse_failure("Expected .", *cursor);
49 skip_whitespaces(str, cursor, n);
51 return parse_failure("EOF", *cursor);
54 struct ParseResult cdr = create_expr_from_str(str, cursor, n);
59 skip_whitespaces(str, cursor, n);
61 return parse_failure("EOF", *cursor);
64 if (str[*cursor] != ')') {
65 return parse_failure("Expected )", *cursor);
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", *cursor);
79 if (isdigit(str[*cursor])) {
80 const char *nptr = str + *cursor;
82 const double x = strtod(nptr, &endptr);
85 return parse_failure("Number expected", *cursor);
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", *cursor);
98 return parse_failure("Unexpected sequence of characters", *cursor);
101 struct ParseResult parse_success(struct Expr expr)
103 struct ParseResult result = {
111 struct ParseResult parse_failure(const char *error,
114 struct ParseResult result = {
117 .error_cursor = error_cursor
123 /* TODO: there is no way to create a pretty report from ParseResult in case of an error */