@@ -75,13 +75,12 @@ pub fn stream_col_at(stream: TokenStream, index: i32): i32
7575
7676pub fn peek_char(src: string, pos: i32): ?i8
7777{
78- if (pos < 0 or pos >= src.length)
78+ if (pos < 0 or src.length <= pos )
7979 {
8080 return None
8181 }
8282 return Some(src[pos])
8383}
84-
8584pub fn is_alpha(c: i8): i32
8685{
8786 if ((c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z') or c == '_') { return 1 }
@@ -108,6 +107,55 @@ pub fn char_string(c: i8): string
108107 return s
109108}
110109
110+ pub fn token_can_end_statement(kind: i32, text: string): i32
111+ {
112+ if (kind == tk_ident() or kind == tk_number() or kind == tk_string() or kind == tk_bool()) { return 1 }
113+ if (text == ")" or text == "]" or text == "}" or text == "break" or text == "continue" or text == "return") { return 1 }
114+ return 0
115+ }
116+
117+ pub fn next_non_space_char(src: string, pos: i32): ?i8
118+ {
119+ let mut i = pos
120+ while (i < src.length)
121+ {
122+ if (src[i] == ' ' or src[i] == '\t' or src[i] == '\r' or src[i] == '\n')
123+ {
124+ i += 1
125+ }
126+ elif (src[i] == '/' and i + 1 < src.length and src[i + 1] == '/')
127+ {
128+ while (i < src.length and src[i] != '\n') { i += 1 }
129+ }
130+ else
131+ {
132+ return Some(src[i])
133+ }
134+ }
135+ return None
136+ }
137+
138+ pub fn newline_should_insert_semicolon(src: string, pos: i32, stream: TokenStream): i32
139+ {
140+ if (stream_len(stream) == 0) { return 0 }
141+ let last = stream_len(stream) - 1
142+ if (token_can_end_statement(stream_kind_at(stream, last), stream_text_at(stream, last)) == 0) { return 0 }
143+ let result = match next_non_space_char(src, pos) {
144+ Some(c) -> {
145+ if (c == '}' or c == '"' or is_alpha(c) == 1 or is_digit(c) == 1)
146+ {
147+ 1
148+ }
149+ else
150+ {
151+ 0
152+ }
153+ }
154+ None -> 0
155+ }
156+ return result
157+ }
158+
111159pub fn is_keyword(text: string): i32
112160{
113161 return match text {
@@ -139,6 +187,10 @@ pub fn tokenize(src: string): TokenStream
139187 elif (c == '\n')
140188 {
141189 pos += 1
190+ if (newline_should_insert_semicolon(src, pos, stream) == 1)
191+ {
192+ stream_push(&stream, tk_symbol(), ";", 0, line, column)
193+ }
142194 line += 1
143195 column = 1
144196 }
0 commit comments