修复了parser解析initexpr模式判断错误的问题。修复标准库改名忘改的问题
This commit is contained in:
@@ -2,3 +2,14 @@ import std.io;
|
||||
|
||||
import token {Token, TokenType};
|
||||
import tokenizer {Tokenizer};
|
||||
|
||||
const src := "abc egaD";
|
||||
const tokenizer := new Tokenizer{src};
|
||||
|
||||
const result := tokenizer.TokenizeAll();
|
||||
|
||||
for var i := 0; i < result.length(); i += 1
|
||||
{
|
||||
const tok := result[i];
|
||||
io.printf("{}: {}\n", tok.literal, tok.type);
|
||||
}
|
||||
@@ -8,13 +8,95 @@ Copyright (C) 2020-2026 PuqiAR
|
||||
|
||||
import token {Token, TokenType};
|
||||
|
||||
func list_contains(lst: List, value: Any) -> Bool
|
||||
{
|
||||
for var i := 0; i < lst.length(); i += 1
|
||||
{
|
||||
if lst[i] == value
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
func isspace(c: String) -> Bool
|
||||
{
|
||||
return c == " " || c == "\n" || c == "\t";
|
||||
}
|
||||
|
||||
func isalpha(c: String) -> Bool
|
||||
{
|
||||
const alb := [
|
||||
"a", "b", "c", "d",
|
||||
"e", "f", "g", "h",
|
||||
"i", "j", "k", "l",
|
||||
"m", "n", "o", "p",
|
||||
"q", "r", "s", "t",
|
||||
"u", "v", "w", "x",
|
||||
"y", "z",
|
||||
"A", "B", "C", "D",
|
||||
"E", "F", "G", "H",
|
||||
"I", "J", "K", "L",
|
||||
"M", "N", "O", "P",
|
||||
"Q", "R", "S", "T",
|
||||
"U", "V", "W", "X",
|
||||
"Y", "Z"
|
||||
];
|
||||
return list_contains(alb, c);
|
||||
}
|
||||
|
||||
|
||||
public struct Tokenizer
|
||||
{
|
||||
src: String = "";
|
||||
idx: Int = 0;
|
||||
|
||||
func next() -> Null
|
||||
{
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
func hasNext() -> Bool
|
||||
{
|
||||
return idx < src.length();
|
||||
}
|
||||
|
||||
func produce() -> String
|
||||
{
|
||||
const tmp := src[idx];
|
||||
idx += 1;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
func current() -> String
|
||||
{
|
||||
return src[idx];
|
||||
}
|
||||
|
||||
public func TokenizeAll() -> List
|
||||
{
|
||||
var output := [];
|
||||
|
||||
const push := func (tok: Token) => output.push(tok);
|
||||
|
||||
while hasNext()
|
||||
{
|
||||
while hasNext() && isspace(current())
|
||||
{
|
||||
next();
|
||||
}
|
||||
if isalpha(current())
|
||||
{
|
||||
var identi := "";
|
||||
while hasNext() && isalpha(current())
|
||||
{
|
||||
identi += produce();
|
||||
}
|
||||
push(new Token{identi, TokenType.Identifier});
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user