commit 70b2cc1df065a909998e5db015454daa8d591c44
parent a3ffc702a1a99f9d827ec0cb0097a5e60d4c7ec2
Author: bsandro <email@bsandro.tech>
Date: Tue, 21 Jun 2022 23:32:51 +0300
parser String { replaced for (
Diffstat:
3 files changed, 31 insertions(+), 23 deletions(-)
diff --git a/ast/ast.go b/ast/ast.go
@@ -46,10 +46,10 @@ func (pe *PrefixExpression) TokenLiteral() string {
}
func (pe *PrefixExpression) String() string {
var out bytes.Buffer
- out.WriteString("{")
+ out.WriteString("(")
out.WriteString(pe.Operator)
out.WriteString(pe.Right.String())
- out.WriteString("}")
+ out.WriteString(")")
return out.String()
}
@@ -66,11 +66,11 @@ func (ie *InfixExpression) TokenLiteral() string {
}
func (ie *InfixExpression) String() string {
var out bytes.Buffer
- out.WriteString("{")
+ out.WriteString("(")
out.WriteString(ie.Left.String())
out.WriteString(" " + ie.Operator + " ")
out.WriteString(ie.Right.String())
- out.WriteString("}")
+ out.WriteString(")")
return out.String()
}
diff --git a/parser/parser_test.go b/parser/parser_test.go
@@ -339,22 +339,22 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
input string
expected string
}{
- {"-a*b", "{{-a} * b}"},
- {"!-a", "{!{-a}}"},
- {"a+b+c", "{{a + b} + c}"},
- {"a*b*c", "{{a * b} * c}"},
- {"a*b/c", "{{a * b} / c}"},
- {"a+b*c+d/e-f", "{{{a + {b * c}} + {d / e}} - f}"},
- {"5>4==3<4", "{{5 > 4} == {3 < 4}}"},
+ {"-a*b", "((-a) * b)"},
+ {"!-a", "(!(-a))"},
+ {"a+b+c", "((a + b) + c)"},
+ {"a*b*c", "((a * b) * c)"},
+ {"a*b/c", "((a * b) / c)"},
+ {"a+b*c+d/e-f", "(((a + (b * c)) + (d / e)) - f)"},
+ {"5>4==3<4", "((5 > 4) == (3 < 4))"},
{"true", "true"},
{"false", "false"},
- {"3<5==true==!false", "{{{3 < 5} == true} == {!false}}"},
- {"!(true == true)", "{!{true == true}}"},
- {"2/(5+5)", "{2 / {5 + 5}}"},
- {"-(5+5)", "{-{5 + 5}}"},
- {"a+add(b*c)+d", "{{a + add({b * c})} + d}"},
- {"add(a+b+c*d/f+g)", "add({{{a + b} + {{c * d} / f}} + g})"},
- {"add(a,b,1,2*3,4+5,add(6,7+8))", "add(a, b, 1, {2 * 3}, {4 + 5}, add(6, {7 + 8}))"},
+ {"3<5==true==!false", "(((3 < 5) == true) == (!false))"},
+ {"!(true == true)", "(!(true == true))"},
+ {"2/(5+5)", "(2 / (5 + 5))"},
+ {"-(5+5)", "(-(5 + 5))"},
+ {"a+add(b*c)+d", "((a + add((b * c))) + d)"},
+ {"add(a+b+c*d/f+g)", "add((((a + b) + ((c * d) / f)) + g))"},
+ {"add(a,b,1,2*3,4+5,add(6,7+8))", "add(a, b, 1, (2 * 3), (4 + 5), add(6, (7 + 8)))"},
}
for _, tt := range tests {
diff --git a/repl/repl.go b/repl/repl.go
@@ -4,7 +4,7 @@ import (
"bufio"
"fmt"
"interp/lexer"
- "interp/token"
+ "interp/parser"
"io"
)
@@ -19,12 +19,20 @@ func Start(in io.Reader, out io.Writer) {
if !scanned {
return
}
-
line := scanner.Text()
l := lexer.New(line)
-
- for tok := l.NextToken(); tok.Type != token.EOF; tok = l.NextToken() {
- fmt.Fprintf(out, "%+v\n", tok)
+ p := parser.New(l)
+ prg := p.ParseProgram()
+ if len(p.Errors()) != 0 {
+ printParserErrors(out, p.Errors())
}
+ io.WriteString(out, prg.String())
+ io.WriteString(out, "\n")
+ }
+}
+
+func printParserErrors(out io.Writer, errors []string) {
+ for _, msg := range errors {
+ io.WriteString(out, "\t"+msg+"\n")
}
}