Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STRING token in right section, DebugString() compact mode #100

Merged
merged 2 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type PrintState struct {

func DebugString(n Node) string {
ps := NewPrintState()
ps.Compact = true
n.PrettyPrint(ps)
return ps.String()
}
Expand Down
14 changes: 7 additions & 7 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func TestFunctionObject(t *testing.T) {
if ast.DebugString(fn.Parameters[0]) != "x" {
t.Fatalf("parameter is not 'x'. got=%q", fn.Parameters[0])
}
expectedBody := "x + 2\n"
expectedBody := "x+2"
got := ast.DebugString(fn.Body)
if got != expectedBody {
t.Fatalf("body is not %q. got=%q", expectedBody, got)
Expand Down Expand Up @@ -615,15 +615,15 @@ func TestQuote(t *testing.T) {
},
{
`quote(5 + 8)`,
`5 + 8`,
`5+8`,
},
{
`quote(foobar)`,
`foobar`,
},
{
`quote(foobar + barfoo)`,
`foobar + barfoo`,
`foobar+barfoo`,
},
}

Expand Down Expand Up @@ -661,11 +661,11 @@ func TestQuoteUnquote(t *testing.T) {
},
{
`quote(8 + unquote(4 + 4))`,
`8 + 8`,
`8+8`,
},
{
`quote(unquote(4 + 4) + 8)`,
`8 + 8`,
`8+8`,
},
{
`foobar = 8;
Expand All @@ -687,12 +687,12 @@ func TestQuoteUnquote(t *testing.T) {
},
{
`quote(unquote(quote(4 + 4)))`,
`4 + 4`,
`4+4`,
},
{
`quotedInfixExpression = quote(4 + 4);
quote(unquote(4 + 4) + unquote(quotedInfixExpression))`,
`8 + (4 + 4)`,
`8+(4+4)`,
},
}
for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion eval/macro_expension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestDefineMacros(t *testing.T) {
t.Fatalf("parameter is not 'y'. got=%q", macro.Parameters[1])
}

expectedBody := "x + y\n" // or should have {}?
expectedBody := "x+y" // or should have {}?
got := ast.DebugString(macro.Body)
if got != expectedBody {
t.Fatalf("body is not %q. got=%q", expectedBody, got)
Expand Down
1 change: 1 addition & 0 deletions examples/fib.gr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* fib.gr - compute the 35th Fibonacci number */
func fib(x) {
if x <= 1 {
x
Expand Down
4 changes: 2 additions & 2 deletions parser/parser_book_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
program := p.ParseProgram()
checkParserErrors(t, p)

actual := ast.DebugString(program)
actual := program.PrettyPrint(ast.NewPrintState()).String()
last := actual[len(actual)-1]
if actual[len(actual)-1] != '\n' {
t.Errorf("expecting newline at end of program output, not found, got %q", last)
Expand Down Expand Up @@ -604,7 +604,7 @@ func TestCallExpressionParameterParsing(t *testing.T) {
{
input: "add(1, 2 * 3, 4 + 5);",
expectedIdent: "add",
expectedArgs: []string{"1", "2 * 3", "4 + 5"},
expectedArgs: []string{"1", "2*3", "4+5"},
},
}

Expand Down
2 changes: 1 addition & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func Test_OperatorPrecedenceParsing(t *testing.T) {
program := p.ParseProgram()
checkParserErrors(t, tt.input, p)

actual := ast.DebugString(program)
actual := program.PrettyPrint(ast.NewPrintState()).String()
last := actual[len(actual)-1]
if actual[len(actual)-1] != '\n' {
t.Errorf("expecting newline at end of program output, not found, got %q", last)
Expand Down
14 changes: 7 additions & 7 deletions token/token.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// There are 2 types of Token, constant ones (with no "value") and the ones with attached
// value that is variable (e.g. IDENT, INT, FLOAT, STRING, COMMENT).
// We'd use the upcoming unique https://tip.golang.org/doc/go1.23#new-unique-package
// but we want this to run on 1.22 and earlier.
// value that is variable (e.g. IDENT, INT, FLOAT, STRING, *COMMENT).
// We might have used the upcoming unique https://tip.golang.org/doc/go1.23#new-unique-package
// but we want this to run on 1.22 and earlier and rolled our own, not multi threaded.
package token

import (
Expand Down Expand Up @@ -56,9 +56,10 @@ const (
startValueTokens

// Identifiers + literals. with attached value.
IDENT // add, foobar, x, y, ...
INT // 1343456
FLOAT // 1. 1e3
IDENT // add, foobar, x, y, ...
INT // 1343456
FLOAT // .5, 3.14159,...
STRING // "foo bar"
LINECOMMENT
BLOCKCOMMENT

Expand Down Expand Up @@ -112,7 +113,6 @@ const (
IF
ELSE
RETURN
STRING
MACRO
// Macro magic.
QUOTE
Expand Down
82 changes: 41 additions & 41 deletions token/type_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.