Skip to content

Commit

Permalink
Merge pull request #5 from JunNishimura/feature/simplify_atom_notation
Browse files Browse the repository at this point in the history
simplify atom notation
  • Loading branch information
JunNishimura authored Sep 21, 2024
2 parents 2541bef + cf76e02 commit 12b3131
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 61 deletions.
8 changes: 1 addition & 7 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@ func (l *Lexer) NextToken() token.Token {
case ',':
tok = newToken(token.COMMA, l.curChar)
case '+':
if isDigit(l.peekChar()) {
l.readChar()
tok.Literal = l.readNumber()
tok.Type = token.INT
} else {
tok = newToken(token.PLUS, l.curChar)
}
tok = newToken(token.PLUS, l.curChar)
case '-':
if isDigit(l.peekChar()) {
l.readChar()
Expand Down
56 changes: 6 additions & 50 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,70 +13,26 @@ func TestSingleProgram(t *testing.T) {
expected []token.Token
}{
{
name: "integer atom",
input: `
{
"atom": 1
}`,
name: "1 digit integer",
input: "1",
expected: []token.Token{
{Type: token.LBRACE, Literal: "{"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.ATOM, Literal: "atom"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.COLON, Literal: ":"},
{Type: token.INT, Literal: "1"},
{Type: token.RBRACE, Literal: "}"},
{Type: token.EOF, Literal: ""},
},
},
{
name: "integer more than 1 digit",
input: `
{
"atom": 123
}`,
expected: []token.Token{
{Type: token.LBRACE, Literal: "{"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.ATOM, Literal: "atom"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.COLON, Literal: ":"},
{Type: token.INT, Literal: "123"},
{Type: token.RBRACE, Literal: "}"},
{Type: token.EOF, Literal: ""},
},
},
{
name: "integer with plus sign",
input: `
{
"atom": +123
}`,
name: "integer more than 1 digit",
input: "123",
expected: []token.Token{
{Type: token.LBRACE, Literal: "{"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.ATOM, Literal: "atom"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.COLON, Literal: ":"},
{Type: token.INT, Literal: "123"},
{Type: token.RBRACE, Literal: "}"},
{Type: token.EOF, Literal: ""},
},
},
{
name: "negative integer",
input: `
{
"atom": -123
}`,
name: "negative integer",
input: "-123",
expected: []token.Token{
{Type: token.LBRACE, Literal: "{"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.ATOM, Literal: "atom"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.COLON, Literal: ":"},
{Type: token.INT, Literal: "-123"},
{Type: token.RBRACE, Literal: "}"},
{Type: token.EOF, Literal: ""},
},
},
Expand Down
4 changes: 0 additions & 4 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ const (
DOUBLE_QUOTE = "\""
COLON = ":"
COMMA = ","

// reserved tokens
ATOM = "ATOM"
)

var reservedWords = map[string]TokenType{
"atom": ATOM,
"command": COMMAND,
"args": ARGS,
}
Expand Down

0 comments on commit 12b3131

Please sign in to comment.