Skip to content

Commit

Permalink
Merge pull request #48 from harp-lab/string_spaces
Browse files Browse the repository at this point in the history
String spaces
  • Loading branch information
thomasgilray authored May 23, 2024
2 parents f4d3b88 + 03eeeca commit dc13f51
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 5 deletions.
7 changes: 5 additions & 2 deletions backend/src/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ enum class slogc_token_type { none,
right_paren,
symbol,
boolean,
number };
number,
string };
enum class slogc_char_type { left_paren,
right_paren,
space,
double_quote,
other };
enum class parse_state { init,
symbol };
symbol,
string };
enum class slogc_relation_version { DELTA,
FULL };

Expand Down
14 changes: 14 additions & 0 deletions backend/src/slog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ slogc_char_type get_char_type(char ch) {
return slogc_char_type::left_paren;
case ')':
return slogc_char_type::right_paren;
case '"':
return slogc_char_type::double_quote;
}
if (isspace(static_cast<unsigned char>(ch)))
return slogc_char_type::space;
Expand Down Expand Up @@ -79,6 +81,16 @@ bool get_token(std::istream &in, slogc_token &tok) {
break;
}
str += ch;
} else if (state == parse_state::string) {
if (ctype == slogc_char_type::double_quote) {
str += ch;
break;
}
str += ch;
} else if (ctype == slogc_char_type::double_quote) {
state = parse_state::string;
type = slogc_token_type::string;
str = ch;
} else if (ctype == slogc_char_type::other) {
state = parse_state::symbol;
type = slogc_token_type::symbol;
Expand All @@ -96,6 +108,8 @@ bool get_token(std::istream &in, slogc_token &tok) {
if (type == slogc_token_type::symbol) {
if (!parse_number(str, tok))
tok.data = str;
} else if (type == slogc_token_type::string){
tok.data = str;
}
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/interpreter.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@
(match key
[`(interned string ,n)
(set! x (max x n))
(displayln (format "~a\t~a" n (hash-ref intern-map key)) out)]
(displayln (format "~a\t\"~a\"" n (hash-ref intern-map key)) out)]
[_ (void)]))
(close-output-port out)
x)
Expand Down
1 change: 0 additions & 1 deletion doc/repl_and_runslog.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,3 @@ Options:
(print-welcome 1)
```
- Here, This is similar to a printf-style debugging, and can be useful in undestanding what each rule is doing in a program.
- Note, Here there are no spaces in the strings, because the current parser does not support spaces in strings.
2 changes: 1 addition & 1 deletion slog/common/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def _update_intern_strings(self, db_id):
if s_line.strip() == '':
continue
sv = s_line.split('\t')[1]
sw_without_quote = f'"{sv.strip()}"'
sw_without_quote = f'{sv.strip()}'
if string_hash(sw_without_quote) in self.intern_string_dict.keys():
if sv.strip() != self.intern_string_dict[string_hash(sw_without_quote)]:
print(f"Hash collision {sv.strip()} {self.intern_string_dict[string_hash(sw_without_quote)]}")
Expand Down
1 change: 1 addition & 0 deletions slog/tests/testcase/stringtest/ground_truth
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
foobar,2,2
just_str,1,5
2 changes: 2 additions & 0 deletions slog/tests/testcase/stringtest/input/strs.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
6 "fact file string"
5 String with spaces from fact file without quotes
8 changes: 8 additions & 0 deletions slog/tests/testcase/stringtest/stringtest.slog
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@
(foo 2 "bar")

[(foobar x y) <-- (foo x y) (bar y)]

; ------------------------------------
; Strings with spaces
(strs 1 "well string")
(strs 2 "well another string)")
(strs 3 "yet another ! ? string")

[(strs _ str_val) --> (just_str str_val)]

0 comments on commit dc13f51

Please sign in to comment.