module support
Some checks failed
Build / build (push) Successful in 10m22s
Test / build (push) Failing after 15m54s

This commit is contained in:
Chuck Smith
2024-03-26 16:49:38 -04:00
parent 6d234099d1
commit 110152a139
21 changed files with 541 additions and 100 deletions

View File

@@ -87,6 +87,7 @@ func New(l *lexer.Lexer) *Parser {
p.registerPrefix(token.LPAREN, p.parseGroupedExpression)
p.registerPrefix(token.IF, p.parseIfExpression)
p.registerPrefix(token.FUNCTION, p.parseFunctionLiteral)
p.registerPrefix(token.IMPORT, p.parseImportExpression)
p.registerPrefix(token.WHILE, p.parseWhileExpression)
p.registerPrefix(token.STRING, p.parseStringLiteral)
p.registerPrefix(token.LBRACKET, p.parseArrayLiteral)
@@ -601,3 +602,20 @@ func (p *Parser) parseBindExpression(expression ast.Expression) ast.Expression {
return be
}
func (p *Parser) parseImportExpression() ast.Expression {
expression := &ast.ImportExpression{Token: p.curToken}
if !p.expectPeek(token.LPAREN) {
return nil
}
p.nextToken()
expression.Name = p.parseExpression(LOWEST)
if !p.expectPeek(token.RPAREN) {
return nil
}
return expression
}

View File

@@ -1227,3 +1227,23 @@ func testComment(t *testing.T, s ast.Statement, expected string) bool {
return true
}
func TestParsingImportExpressions(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string
expected string
}{
{`import("mod")`, `import("mod")`},
}
for _, tt := range tests {
l := lexer.New(tt.input)
p := New(l)
program := p.ParseProgram()
checkParserErrors(t, p)
assert.Equal(tt.expected, program.String())
}
}