Skip to content

Commit fec8f59

Browse files
committed
feat(vixc) del <> for generic syntax , use :[]
1 parent 37e8f4a commit fec8f59

3 files changed

Lines changed: 98 additions & 3 deletions

File tree

vixc0/lexer.vix

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,12 @@ pub fn stream_col_at(stream: TokenStream, index: i32): i32
7575

7676
pub 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-
8584
pub 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+
111159
pub 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
}

vixc0/main.vix

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,47 @@ import "parser.vix"
33
import "ast.vix"
44
import "codegen.vix"
55

6+
extern "C"
7+
{
8+
fn fopen(filename: ptr, mode: ptr): ptr
9+
fn fclose(file: ptr): i32
10+
fn fread(buf: ptr, size: i64, count: i64, file: ptr): i64
11+
fn fseek(file: ptr, offset: i64, whence: i32): i32
12+
fn ftell(file: ptr): i64
13+
fn malloc(size: usize): string
14+
}
15+
16+
fn string_ends_with(text: string, suffix: string): i32
17+
{
18+
if (text.length < suffix.length) { return 0 }
19+
let offset = text.length - suffix.length
20+
for (i in 0 .. suffix.length)
21+
{
22+
if (text[offset + i] != suffix[i]) { return 0 }
23+
}
24+
return 1
25+
}
26+
27+
fn read_source_file(path: ptr): string
28+
{
29+
let file = fopen(path, "r")
30+
if (file == 0)
31+
{
32+
return ""
33+
}
34+
fseek(file, 0, 2)
35+
let size = ftell(file)
36+
fseek(file, 0, 0)
37+
let buf = malloc(size + 1)
38+
let n = fread(buf, 1, size, file)
39+
buf[n] = 0
40+
fclose(file)
41+
return buf
42+
}
43+
644
fn main(argc: i32, argv: ptr): i32
745
{
8-
let mut src = "fn main():i32 { let a = 10 let b = 20 let c = 30 return 0 }"
46+
let mut src = "fn main():i32 { let a = 10; let b = 20; let c = 30; printf(a); if (a < 100) { printf(a); } return 0 }"
947
let mut mode = "codegen"
1048

1149
if (argc > 1)
@@ -15,6 +53,10 @@ fn main(argc: i32, argv: ptr): i32
1553
if (argc > 2)
1654
{
1755
src = argv[2]
56+
if (string_ends_with(src, ".vix") == 1)
57+
{
58+
src = read_source_file(argv[2])
59+
}
1860
}
1961

2062
if (mode == "--lex")

vixc0/parser.vix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ pub fn parse_unary(parser: &Parser): i32
180180
}
181181
_ -> parse_primary(parser)
182182
}
183+
183184
return result
184185
}
185186

0 commit comments

Comments
 (0)