]> git.lizzy.rs Git - nothing.git/commitdiff
(#288) Implement string parsing
authorrexim <reximkut@gmail.com>
Sat, 25 Aug 2018 17:50:44 +0000 (00:50 +0700)
committerrexim <reximkut@gmail.com>
Sat, 25 Aug 2018 17:50:44 +0000 (00:50 +0700)
src/script/parser.c
src/script_test.c

index 5398fe9a68195672f1cd9e7e89eac4a74c6be2f6..66a69b97177b449069fe25c0625fa0881410b90a 100644 (file)
@@ -71,8 +71,23 @@ struct ParseResult create_expr_from_str(const char *str,
     }
 
     case '"': {
-        /* TODO(#288): create_expr_from_str does not support strings */
-        return parse_failure("Strings are not supported", *cursor);
+        /* TODO: parser does not support escaped string characters */
+        const size_t str_begin = *cursor + 1;
+        size_t str_end = str_begin;
+
+        while(str_end < n && str[str_end] != '"') {
+            str_end++;
+        }
+
+        if (str_end >= n) {
+            return parse_failure("Unclosed string", str_begin);
+        }
+
+        *cursor = str_end + 1;
+
+        return parse_success(
+            atom_as_expr(
+                create_string_atom(str + str_begin, str + str_end)));
     }
 
     default: {
index d0264ce8f8eff112c4aac2fceda5e221585b29cf..d084e6ee770c7c2911470d0aff034df04eea0b19 100644 (file)
@@ -8,7 +8,7 @@ int main(int argc, char *argv[])
     (void) argc;
     (void) argv;
 
-    const char *code = "(1 . (2 . 3))";
+    const char *code = "(1 . (\"2\" . 3))";
     size_t cursor = 0;
     const size_t n = strlen(code);