refactor how repl works
This commit is contained in:
@@ -61,6 +61,7 @@ type (
|
||||
)
|
||||
|
||||
type Parser struct {
|
||||
fn string
|
||||
l *lexer.Lexer
|
||||
errors []string
|
||||
|
||||
@@ -71,8 +72,9 @@ type Parser struct {
|
||||
infixParseFns map[token.TokenType]infixParseFn
|
||||
}
|
||||
|
||||
func New(l *lexer.Lexer) *Parser {
|
||||
func New(fn string, l *lexer.Lexer) *Parser {
|
||||
p := &Parser{
|
||||
fn: fn,
|
||||
l: l,
|
||||
errors: []string{},
|
||||
}
|
||||
@@ -619,3 +621,11 @@ func (p *Parser) parseImportExpression() ast.Expression {
|
||||
|
||||
return expression
|
||||
}
|
||||
|
||||
// Parse parses the input source into a top-level AST for either evaluation
|
||||
// or compilation to bytecode. The parameter fn denotes the filename the source
|
||||
// originated from.
|
||||
func Parse(fn, input string) (prog ast.Node, errors []string) {
|
||||
p := New(fn, lexer.New(input))
|
||||
return p.ParseProgram(), p.Errors()
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestBindExpressions(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
l := lexer.New(tt.input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -40,7 +40,7 @@ func TestReturnStatements(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
l := lexer.New(tt.input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -68,7 +68,7 @@ func TestIdentifierExpression(t *testing.T) {
|
||||
input := "foobar;"
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -99,7 +99,7 @@ func TestIntegerLiteralExpression(t *testing.T) {
|
||||
input := "5;"
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -143,7 +143,7 @@ func TestParsingPrefixExpressions(t *testing.T) {
|
||||
|
||||
for _, tt := range prefixTests {
|
||||
l := lexer.New(tt.input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -202,7 +202,7 @@ func TestParsingInfixExpressions(t *testing.T) {
|
||||
|
||||
for _, tt := range infixTests {
|
||||
l := lexer.New(tt.input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -353,7 +353,7 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
l := lexer.New(tt.input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -375,7 +375,7 @@ func TestBooleanExpression(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
l := lexer.New(tt.input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -405,7 +405,7 @@ func TestIfExpression(t *testing.T) {
|
||||
input := `if (x < y) { x }`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -452,7 +452,7 @@ func TestIfExpression(t *testing.T) {
|
||||
|
||||
func TestNullExpression(t *testing.T) {
|
||||
l := lexer.New("null")
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -477,7 +477,7 @@ func TestIfElseExpression(t *testing.T) {
|
||||
input := `if (x < y) { x } else { y }`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -536,7 +536,7 @@ func TestIfElseIfExpression(t *testing.T) {
|
||||
input := `if (x < y) { x } else if (x == y) { y }`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -615,7 +615,7 @@ func TestFunctionLiteralParsing(t *testing.T) {
|
||||
input := `fn(x, y) { x + y; }`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -670,7 +670,7 @@ func TestFunctionParameterParsing(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
l := lexer.New(tt.input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -692,7 +692,7 @@ func TestCallExpressionParsing(t *testing.T) {
|
||||
input := "add(1, 2 * 3, 4 + 5);"
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -751,7 +751,7 @@ func TestCallExpressionParameterParsing(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
l := lexer.New(tt.input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -784,7 +784,7 @@ func TestStringLiteralExpression(t *testing.T) {
|
||||
input := `"hello world";`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -803,7 +803,7 @@ func TestParsingArrayLiterals(t *testing.T) {
|
||||
input := "[1, 2 * 2, 3 + 3]"
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -826,7 +826,7 @@ func TestParsingSelectorExpressions(t *testing.T) {
|
||||
input := "myHash.foo"
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
||||
@@ -860,7 +860,7 @@ func TestParsingIndexExpressions(t *testing.T) {
|
||||
input := "myArray[1 + 1]"
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -883,7 +883,7 @@ func TestParsingHashLiteralsStringKeys(t *testing.T) {
|
||||
input := `{"one": 1, "two": 2, "three": 3}`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -919,7 +919,7 @@ func TestParsingEmptyHashLiteral(t *testing.T) {
|
||||
input := "{}"
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -938,7 +938,7 @@ func TestParsingHashLiteralsWithExpressions(t *testing.T) {
|
||||
input := `{"one": 0 + 1, "two": 10 - 8, "three": 15 / 5}`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -987,7 +987,7 @@ func TestFunctionDefinitionParsing(t *testing.T) {
|
||||
input := `add := fn(x, y) { x + y; }`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -998,7 +998,7 @@ func TestWhileExpression(t *testing.T) {
|
||||
input := `while (x < y) { x }`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -1055,7 +1055,7 @@ func TestAssignmentExpressions(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
l := lexer.New(tt.input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -1197,7 +1197,7 @@ func TestComments(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
l := lexer.New(tt.input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
@@ -1240,7 +1240,7 @@ func TestParsingImportExpressions(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
l := lexer.New(tt.input)
|
||||
p := New(l)
|
||||
p := New("<test>", l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user