c interop type foundation
This commit is contained in:
+101
-20
@@ -14,16 +14,44 @@ is_identifier_continue :: proc(value: byte) -> bool {
|
||||
|
||||
keyword_kind :: proc(text: string) -> token.Kind {
|
||||
switch text {
|
||||
case "func": return .Keyword_Func
|
||||
case "import": return .Keyword_Import
|
||||
case "return": return .Keyword_Return
|
||||
case "void": return .Keyword_Void
|
||||
case "int": return .Keyword_Int
|
||||
case "i8": return .Keyword_I8
|
||||
case "i16": return .Keyword_I16
|
||||
case "i32": return .Keyword_I32
|
||||
case "i64": return .Keyword_I64
|
||||
case "_": return .Underscore
|
||||
case "func": return .Keyword_Func
|
||||
case "c_func": return .Keyword_C_Func
|
||||
case "struct": return .Keyword_Struct
|
||||
case "c_struct": return .Keyword_C_Struct
|
||||
case "import": return .Keyword_Import
|
||||
case "return": return .Keyword_Return
|
||||
case "mut": return .Keyword_Mut
|
||||
case "none": return .Keyword_None
|
||||
case "orelse": return .Keyword_Orelse
|
||||
case "void": return .Keyword_Void
|
||||
case "int": return .Keyword_Int
|
||||
case "i8": return .Keyword_I8
|
||||
case "i16": return .Keyword_I16
|
||||
case "i32": return .Keyword_I32
|
||||
case "i64": return .Keyword_I64
|
||||
case "u8": return .Keyword_U8
|
||||
case "u16": return .Keyword_U16
|
||||
case "u32": return .Keyword_U32
|
||||
case "u64": return .Keyword_U64
|
||||
case "isize": return .Keyword_Isize
|
||||
case "usize": return .Keyword_Usize
|
||||
case "f32": return .Keyword_F32
|
||||
case "f64": return .Keyword_F64
|
||||
case "c_char": return .Keyword_C_Char
|
||||
case "c_schar": return .Keyword_C_Schar
|
||||
case "c_uchar": return .Keyword_C_Uchar
|
||||
case "c_short": return .Keyword_C_Short
|
||||
case "c_ushort": return .Keyword_C_Ushort
|
||||
case "c_int": return .Keyword_C_Int
|
||||
case "c_uint": return .Keyword_C_Uint
|
||||
case "c_long": return .Keyword_C_Long
|
||||
case "c_ulong": return .Keyword_C_Ulong
|
||||
case "c_longlong": return .Keyword_C_Longlong
|
||||
case "c_ulonglong": return .Keyword_C_Ulonglong
|
||||
case "c_float": return .Keyword_C_Float
|
||||
case "c_double": return .Keyword_C_Double
|
||||
case "c_longdouble": return .Keyword_C_Longdouble
|
||||
case "_": return .Underscore
|
||||
}
|
||||
return .Identifier
|
||||
}
|
||||
@@ -87,7 +115,34 @@ lex :: proc(
|
||||
append_token(&stream, source_file, .Minus, cursor, cursor+1)
|
||||
cursor += 1
|
||||
case '.':
|
||||
append_token(&stream, source_file, .Dot, cursor, cursor+1)
|
||||
start := cursor
|
||||
cursor += 1
|
||||
if cursor < len(bytes) && bytes[cursor] == '.' {
|
||||
cursor += 1
|
||||
append_token(&stream, source_file, .Range, start, cursor)
|
||||
} else {
|
||||
append_token(&stream, source_file, .Dot, start, cursor)
|
||||
}
|
||||
case '@':
|
||||
append_token(&stream, source_file, .At, cursor, cursor+1)
|
||||
cursor += 1
|
||||
case '*':
|
||||
append_token(&stream, source_file, .Star, cursor, cursor+1)
|
||||
cursor += 1
|
||||
case '&':
|
||||
append_token(&stream, source_file, .Ampersand, cursor, cursor+1)
|
||||
cursor += 1
|
||||
case '^':
|
||||
append_token(&stream, source_file, .Caret, cursor, cursor+1)
|
||||
cursor += 1
|
||||
case '?':
|
||||
append_token(&stream, source_file, .Question, cursor, cursor+1)
|
||||
cursor += 1
|
||||
case '[':
|
||||
append_token(&stream, source_file, .Left_Bracket, cursor, cursor+1)
|
||||
cursor += 1
|
||||
case ']':
|
||||
append_token(&stream, source_file, .Right_Bracket, cursor, cursor+1)
|
||||
cursor += 1
|
||||
case '(':
|
||||
append_token(&stream, source_file, .Left_Paren, cursor, cursor+1)
|
||||
@@ -111,11 +166,13 @@ lex :: proc(
|
||||
for cursor < len(bytes) && bytes[cursor] != '"' && bytes[cursor] != '\n' {
|
||||
if bytes[cursor] == '\\' {
|
||||
cursor += 1
|
||||
if cursor >= len(bytes) || (bytes[cursor] != '\\' && bytes[cursor] != '"') {
|
||||
if cursor >= len(bytes) ||
|
||||
(bytes[cursor] != '\\' && bytes[cursor] != '"' && bytes[cursor] != 'n' &&
|
||||
bytes[cursor] != 'r' && bytes[cursor] != 't' && bytes[cursor] != '0') {
|
||||
source.add(
|
||||
diagnostics,
|
||||
source.Span{file=source_file.id, start=source.Offset(max(cursor-1, start)), end=source.Offset(min(cursor+1, len(bytes)))},
|
||||
"import strings only support '\\\\' and '\\\"' escapes",
|
||||
"strings only support '\\\\', '\\\"', '\\n', '\\r', '\\t', and '\\0' escapes",
|
||||
)
|
||||
valid = false
|
||||
}
|
||||
@@ -136,20 +193,44 @@ lex :: proc(
|
||||
append_token(&stream, source_file, .Invalid, start, cursor, diagnostic=id)
|
||||
}
|
||||
case ';':
|
||||
id := source.add(
|
||||
diagnostics,
|
||||
source.Span{file=source_file.id, start=source.Offset(cursor), end=source.Offset(cursor+1)},
|
||||
"semicolons are invalid; terminate statements with a newline",
|
||||
)
|
||||
append_token(&stream, source_file, .Invalid, cursor, cursor+1, diagnostic=id)
|
||||
append_token(&stream, source_file, .Semicolon, cursor, cursor+1)
|
||||
cursor += 1
|
||||
case '\'':
|
||||
start := cursor
|
||||
cursor += 1
|
||||
for cursor < len(bytes) && bytes[cursor] != '\'' && bytes[cursor] != '\n' {
|
||||
if bytes[cursor] == '\\' && cursor+1 < len(bytes) {
|
||||
cursor += 1
|
||||
}
|
||||
cursor += 1
|
||||
}
|
||||
if cursor < len(bytes) && bytes[cursor] == '\'' {
|
||||
cursor += 1
|
||||
append_token(&stream, source_file, .Character, start, cursor)
|
||||
} else {
|
||||
id := source.add(
|
||||
diagnostics,
|
||||
source.Span{file=source_file.id, start=source.Offset(start), end=source.Offset(cursor)},
|
||||
"unterminated character literal",
|
||||
)
|
||||
append_token(&stream, source_file, .Invalid, start, cursor, diagnostic=id)
|
||||
}
|
||||
case:
|
||||
if value >= '0' && value <= '9' {
|
||||
start := cursor
|
||||
for cursor < len(bytes) && bytes[cursor] >= '0' && bytes[cursor] <= '9' {
|
||||
cursor += 1
|
||||
}
|
||||
append_token(&stream, source_file, .Integer, start, cursor)
|
||||
kind := token.Kind.Integer
|
||||
if cursor+1 < len(bytes) && bytes[cursor] == '.' &&
|
||||
bytes[cursor+1] != '.' && bytes[cursor+1] >= '0' && bytes[cursor+1] <= '9' {
|
||||
kind = .Float
|
||||
cursor += 1
|
||||
for cursor < len(bytes) && bytes[cursor] >= '0' && bytes[cursor] <= '9' {
|
||||
cursor += 1
|
||||
}
|
||||
}
|
||||
append_token(&stream, source_file, kind, start, cursor)
|
||||
} else if is_identifier_start(value) {
|
||||
start := cursor
|
||||
for cursor < len(bytes) && is_identifier_continue(bytes[cursor]) {
|
||||
|
||||
Reference in New Issue
Block a user