Latest changes back from 2021, before starting from scratch.

This commit is contained in:
2024-01-11 02:55:07 +03:00
parent b8495b4cc3
commit 6bdc8cce09
64 changed files with 2113 additions and 1577 deletions

View File

@@ -14,4 +14,5 @@ main {
stdio.println("\n")
}
# by Sdore, 2020
# by Sdore, 2021
# slang.sdore.me

View File

@@ -3,7 +3,8 @@
import tokens:*
tuple read_token(str src, int lineno, int offset, int lineoff) {
tuple c = lstripcount(src#|[offset:]|#, whitespace)
tuple c = lstripcount(src[offset:], whitespace)
int l = c[0]
src = c[1]
str line = src
@@ -11,39 +12,42 @@ tuple read_token(str src, int lineno, int offset, int lineoff) {
if not src or src[0] in '\n;' {
return (offset, None)
}
int length = 0
int ii = -1
int length = 0, ii = -1
for i in Token.types {
ii += 1
auto r = globals['find_'+i.casefold()](src) or 0
if r isof int and r <= 0 {
length = max(length, -r)
continue
}
tuple c
if r isof tuple {
c = r
}
else {
c = (r, src[:r])
}
if r isof tuple: c = r
else: c = (r, src[:r])
int n = c[0]
str s = c[1]
return (offset+n, Token(ii, s, lineno=lineno, offset=offset+lineoff))
} #else raise SlSyntaxError("Invalid token", line, lineno=lineno, offset=offset+lineoff, length=length)
} else: raise SlSyntaxError("Invalid token", line, lineno=lineno, offset=offset+lineoff, length=length)
return (0, Token())
}
tuple parse_expr(str src, int lineno = 1, int lineoff = 0) {
list r = [Token]
int offset
while 1 {
while true {
offset, tok = read_token(src, lineno=lineno, offset=offset, lineoff=lineoff)
if tok is None {
break
}
if tok is None: break
r.append(tok)
}
return (offset, r)
}
@@ -52,23 +56,25 @@ list parse_string(str src) {
list tl = [Token]
int lines = src.count('\n')
int lineoff = 0
while src {
tuple c = parse_expr(src, lineno=lines-src.count('\n')+1, lineoff=lineoff)
int offset = c[0]
list r = c[1]
lineoff += offset
if offset < src.len {
if src[offset] == '\n' {
lineoff = 0
}
else {
lineoff += 1
}
if src[offset] == '\n': lineoff = 0
else: lineoff += 1
}
src = src[offset+1:]
tl.append(r)
}
return tl
}
# by Sdore, 2020
# by Sdore, 2021
# slang.sdore.me

View File

@@ -1,49 +1,35 @@
# Slang tokens
str whitespace = ' \t\r\v\f'
const str whitespace = " \t\r\v\f"
tuple lstripcount(str s, str chars) {
int ii = -1
char i
for i in s {
ii += 1
if i not in chars {
break
}
}
else {
ii = 0
}
return (ii, s#|[ii:]|#)
if i not in chars: break
} else: ii = 0
return (ii, s[ii:])
}
class Token {
int type
const tuple types = ('SPECIAL', 'OPERATOR', 'LITERAL', 'KEYWORD', 'IDENTIFIER') # order is also resolution order
int type, lineno, offset
str token
int lineno
int offset
tuple types# = ('SPECIAL', 'OPERATOR', 'LITERAL', 'KEYWORD', 'IDENTIFIER') # order is also resolution order
constr (int .type, str .token, int .lineno, int .offset);
constr(int type, str token, int lineno, int offset) {
#.type, .token, .lineno, .offset = type, token, lineno, offset
}
repr = f"<Token {.typename} «{repr(.token)[1:-1]}» at line {.lineno}, offset {.offset}>"
#|repr {
return "<Token {.types[.type]} «{repr(.token)[1:-1]}» at line {.lineno}, offset {.offset}>"
}|#
eq = (super == x or .token == x)
#|eq {
#return super() == x or .token == x
}|#
property typename = .types[.type]
#|property typename {
#return .types[.type]
}
property length {
#return .token.length
}|#
property length = .token.length
}
# by Sdore, 2020
# by Sdore, 2021
# slang.sdore.me