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

a=1; -a returns an invalid value #568

Open
hachi8833 opened this issue Jan 7, 2018 · 7 comments
Open

a=1; -a returns an invalid value #568

hachi8833 opened this issue Jan 7, 2018 · 7 comments
Assignees
Labels

Comments

@hachi8833
Copy link
Member

Found the issue in #560, on REPL.
Unary operator + or - just after semicolon ; returns invalid values like:

» a=1;a
#» 1    # correct
» a=1; -a
#» 0    # invalid: should be -1
» a=1;+a
#» 2    # invalid: should be 1
» a
#» 1    # correct
» -a
#» -1  # correct
@hachi8833
Copy link
Member Author

I have an idea to fix the issue: how about changing skipWhitespace() to treat ; as \n like the following? This would make the lexer simplified.

func (l *Lexer) skipWhitespace() {
	for l.ch == ' ' || l.ch == '\t' || l.ch == '\r' || l.ch == '\n' || l.ch == ';' {
		if l.ch == '\n' || l.ch == ';' {
			l.line++
		}
		l.readChar()
	}
}

@st0012
Copy link
Member

st0012 commented Jan 8, 2018

I think this is just a workaround, not the best solution

@SD10
Copy link
Member

SD10 commented Jan 17, 2018

It looks like when the unary operator is present, these are being treated as InfixExpression 🤔

» a="1";.to_i
#» 1

@st0012
Copy link
Member

st0012 commented Jul 27, 2019

Can you help me confirm this as well?

@hachi8833
Copy link
Member Author

Got it!

@hachi8833 hachi8833 self-assigned this Jul 27, 2019
@Alexius-Huang
Copy link
Member

I know the test below is incorrect, but the result below should be clear on how it was parsed:

// parser_test.go
func TestMultipleExpressionOnSameLineParsing(t *testing.T) {
	tests := []struct {
		input    string
		expected string
	}{
		{
			"a = 1; +a",
			"a = 1; +a",
		},
		{
			"a = 1; .to_i",
			"a = 1; .to_i",
		},
	}
        // ... omit

The result:

// ...omit above
ok      github.com/goby-lang/goby/compiler/lexer        (cached)
--- FAIL: TestMultipleExpressionOnSameLineParsing (0.00s)
    parser_test.go:198: expcted="a = 1; +a", got="(a = 1 + a)"
    parser_test.go:198: expcted="a = 1; .to_i", got="a = 1.to_i()"
FAIL
// ...omit below

(However, I'm still thinking about how should the problem be solved. 🤔 )

@st0012
Copy link
Member

st0012 commented Oct 27, 2019

@Maxwell-Alexius great observation! According to the failure message, I think the parser just ignored the semicolon accidentally.

@st0012 st0012 added this to the version 0.1.12 milestone Oct 27, 2019
@hachi8833 hachi8833 mentioned this issue Oct 28, 2019
@st0012 st0012 modified the milestones: version 0.1.12, version 0.1.13 Feb 16, 2020
@st0012 st0012 modified the milestones: version 0.1.13, version 0.1.14 Apr 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants