12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package parser
- import (
- "testing"
- "code.osinet.fr/fgm/waiig15/lexer"
- )
- func checkParserErrors(t *testing.T, p *Parser) {
- errors := p.Errors()
- if len(errors) == 0 {
- return
- }
- t.Errorf("parser has %d errors", len(errors))
- for _, msg := range errors {
- t.Errorf("parser error: %q", msg)
- }
- t.FailNow()
- }
- func TestOperatorPrecedenceParsing(t *testing.T) {
- tests := []struct {
- 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",
- "((a * b) / c)",
- },
- {
- "a + b / c",
- "(a + (b / c))",
- },
- {
- "a + b * c + d / e - f",
- "(((a + (b * c)) + (d / e)) - f)",
- },
- {
- "3 + 4; -5 * 5",
- "(3 + 4)((-5) * 5)",
- },
- {
- "5 > 4 == 3 < 4",
- "((5 > 4) == (3 < 4))",
- },
- {
- "5 < 4 != 3 > 4",
- "((5 < 4) != (3 > 4))",
- },
- {
- "3 + 4 * 5 == 3 * 1 + 4 * 5",
- "((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))",
- },
- }
- for _, tt := range tests {
- l := lexer.New(tt.input)
- p := New(l)
- program := p.ParseProgram()
- checkParserErrors(t, p)
- actual := program.String()
- if actual != tt.expected {
- t.Errorf("expected=%q, got=%q", tt.expected, actual)
- }
- }
- }
|