Skip to content

Commit

Permalink
Merge pull request #46 from JunNishimura/feature/add_single_line_comment
Browse files Browse the repository at this point in the history
add single line comment
  • Loading branch information
JunNishimura authored Oct 8, 2024
2 parents a2e0201 + 3caf681 commit db0a22c
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
12 changes: 12 additions & 0 deletions evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ func TestEvalIntegerExpression(t *testing.T) {
`,
expected: 2,
},
{
name: "single line comment",
input: `
{
"command": {
"//": "this is a comment",
"symbol": "+",
"args": [1, 2]
}
}`,
expected: 3,
},
}

for _, tt := range tests {
Expand Down
45 changes: 45 additions & 0 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,51 @@ func TestSingleProgram(t *testing.T) {
{Type: token.EOF, Literal: ""},
},
},
{
name: "single line comment",
input: `
{
"//": "this is a comment",
"command": {
"//": "this is another comment",
"symbol": "print"
}
}`,
expected: []token.Token{
{Type: token.LBRACE, Literal: "{"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.STRING, Literal: "//"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.COLON, Literal: ":"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.STRING, Literal: "this is a comment"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.COMMA, Literal: ","},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.STRING, Literal: "command"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.COLON, Literal: ":"},
{Type: token.LBRACE, Literal: "{"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.STRING, Literal: "//"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.COLON, Literal: ":"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.STRING, Literal: "this is another comment"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.COMMA, Literal: ","},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.STRING, Literal: "symbol"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.COLON, Literal: ":"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.STRING, Literal: "print"},
{Type: token.DOUBLE_QUOTE, Literal: "\""},
{Type: token.RBRACE, Literal: "}"},
{Type: token.RBRACE, Literal: "}"},
{Type: token.EOF, Literal: ""},
},
},
}

for _, tt := range tests {
Expand Down
66 changes: 66 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,72 @@ func TestCommand(t *testing.T) {
},
},
},
{
name: "single line comment",
input: `
{
"command": {
"//": "this is a comment",
"symbol": "+",
"args": [1, 2]
}
}`,
expected: &ast.KeyValueObject{
Token: token.Token{Type: token.LBRACE, Literal: "{"},
KV: []*ast.KeyValuePair{
{
Key: &ast.StringLiteral{
Token: token.Token{Type: token.STRING, Literal: "command"},
Value: "command",
},
Value: &ast.KeyValueObject{
Token: token.Token{Type: token.LBRACE, Literal: "{"},
KV: []*ast.KeyValuePair{
{
Key: &ast.StringLiteral{
Token: token.Token{Type: token.STRING, Literal: "//"},
Value: "//",
},
Value: &ast.StringLiteral{
Token: token.Token{Type: token.STRING, Literal: "this is a comment"},
Value: "this is a comment",
},
},
{
Key: &ast.StringLiteral{
Token: token.Token{Type: token.STRING, Literal: "symbol"},
Value: "symbol",
},
Value: &ast.Symbol{
Token: token.Token{Type: token.STRING, Literal: "+"},
Value: "+",
},
},
{
Key: &ast.StringLiteral{
Token: token.Token{Type: token.STRING, Literal: "args"},
Value: "args",
},
Value: &ast.Array{
Token: token.Token{Type: token.LBRACKET, Literal: "["},
Elements: []ast.Expression{
&ast.IntegerLiteral{
Token: token.Token{Type: token.INT, Literal: "1"},
Value: 1,
},
&ast.IntegerLiteral{
Token: token.Token{Type: token.INT, Literal: "2"},
Value: 2,
},
},
},
},
},
},
},
},
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit db0a22c

Please sign in to comment.