aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeer <himbeer@disroot.org>2024-08-30 22:11:17 +0200
committerHimbeer <himbeer@disroot.org>2024-08-30 22:11:17 +0200
commit4019e81876fdceccf2db58182160d996546adaa5 (patch)
tree21d63376d95ca930496e6030ed7eddd0b6340733
parent0e806f6a119f3f4ee31d3b52ec8eaf4415f2da4e (diff)
Remove the "assign" keyword from assignment syntax
-rw-r--r--GRAMMAR.txt2
-rw-r--r--lex.go2
-rw-r--r--parse.go34
-rw-r--r--token.go3
4 files changed, 21 insertions, 20 deletions
diff --git a/GRAMMAR.txt b/GRAMMAR.txt
index 4882ff8..bd7b4d4 100644
--- a/GRAMMAR.txt
+++ b/GRAMMAR.txt
@@ -17,7 +17,7 @@ declaration -> constant | mutable
constant -> "const" IDENTIFIER "=" expression
mutable -> "mut" IDENTIFIER "=" expression
-assignment -> "assign" IDENTIFIER "=" expression
+assignment -> IDENTIFIER "=" expression
expression -> equality
equality -> comparison ( ( "==" | "!=" ) comparison )*
diff --git a/lex.go b/lex.go
index 961291a..7450888 100644
--- a/lex.go
+++ b/lex.go
@@ -300,8 +300,6 @@ func addKeywordOrIdentifier(name string, line int) token {
return token{kind: constKeyword, line: line}
case "mut":
return token{kind: mut, line: line}
- case "assign":
- return token{kind: assign, line: line}
default:
return token{kind: identifier, value: name, line: line}
}
diff --git a/parse.go b/parse.go
index a9c7b9d..7b4469e 100644
--- a/parse.go
+++ b/parse.go
@@ -24,7 +24,11 @@ func (t *tokens) consumeToken() (token, bool) {
}
func (t *tokens) unreadToken() {
- t.index--
+ t.unreadTokens(1)
+}
+
+func (t *tokens) unreadTokens(n int) {
+ t.index -= n
}
func (t *tokens) current() token {
@@ -396,32 +400,34 @@ func parseMutStmt(toks *tokens) (*mutStmt, error) {
}
func parseAssignStmt(toks *tokens) (*assignStmt, error) {
- ok, err := toks.match(assign)
- if err != nil {
- return nil, err
- }
- if !ok {
- toks.unreadToken()
- return nil, nil
- }
-
- line := toks.current().line
-
nameTok, ok := toks.consumeToken()
if !ok {
return nil, unexpectedEOF
}
+ if nameTok.kind != identifier {
+ toks.unreadToken()
+ return nil, nil
+ }
- if err := toks.mustMatch(equals); err != nil {
+ ok, err := toks.match(equals)
+ if err != nil {
return nil, err
}
+ if !ok {
+ toks.unreadTokens(2)
+ return nil, nil
+ }
expr, err := parseExpression(toks)
if err != nil {
return nil, err
}
- return &assignStmt{name: nameTok.value, value: expr, ln: line}, nil
+ return &assignStmt{
+ name: nameTok.value,
+ value: expr,
+ ln: nameTok.line,
+ }, nil
}
func parseExpression(toks *tokens) (exprExpr, error) {
diff --git a/token.go b/token.go
index 14d23a1..20d4727 100644
--- a/token.go
+++ b/token.go
@@ -104,8 +104,6 @@ func (k tokenKind) String() string {
return "const"
case mut:
return "mut"
- case assign:
- return "assign"
}
return "invalid token"
@@ -161,7 +159,6 @@ const (
ret
constKeyword
mut
- assign
)
type token struct {