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

[WIP]Feature/issue 660 #666

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions compiler/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,29 @@ func (l *Lexer) resetNosymbol() {

}

// prevent double underscores and underscore at the end such as 1__2, 3_
func (l *Lexer) checkNumberError() bool {

return l.ch == '_' && (l.readPosition >= len(l.input) || !isDigit(l.input[l.readPosition]))

}

func (l *Lexer) readNumber() []rune {
position := l.position
for isDigit(l.ch) {
var input []rune

for isDigit(l.ch) || l.ch == '_' {

if isDigit(l.ch) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO I think the lexer should preserve the underscores in the token. In terms of responsibility, I don't think a lexer should be responsible for any code transformation. Removing them at this stage seems sort of magical.

IMO the token should be simplified at a later stage, maybe during parsing or eval?

input = append(input, l.ch)
}

if l.checkNumberError() {
return input
}

l.readChar()
}
return l.input[position:l.position]
return input
}

func (l *Lexer) readIdentifier() []rune {
Expand Down
9 changes: 7 additions & 2 deletions compiler/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func TestNextToken(t *testing.T) {
'\"string\"'
"\'string\'"
'\'string\''
1_23
12_3.45_6
`

tests := []struct {
Expand Down Expand Up @@ -438,8 +440,11 @@ func TestNextToken(t *testing.T) {
{token.String, "\\\"string\\\"", 116},
{token.String, "'string'", 117},
{token.String, "'string'", 118},

{token.EOF, "", 119},
{token.Int, "123", 119},
{token.Int, "123", 120},
{token.Dot, ".", 120},
{token.Int, "456", 120},
{token.EOF, "", 121},
}
l := New(input)

Expand Down
35 changes: 35 additions & 0 deletions vm/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,38 @@ func TestFloatZeroDivisionFail(t *testing.T) {
v.checkSP(t, i, 1)
}
}

func TestFloatWithWrongUnderscore(t *testing.T) {
testsFail := []errorTestCase{
{`1._2`, "UndefinedMethodError: Undefined Method '_2' for 1", 1},
{`1.2_`, "UndefinedMethodError: Undefined Method '_' for <Instance of: Object>", 1},
}
for i, tt := range testsFail {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
checkErrorMsg(t, i, evaluated, tt.expected)
v.checkCFP(t, i, tt.expectedCFP)
v.checkSP(t, i, 1)
}
}

func TestFloatWithCorrectUnderscore(t *testing.T) {

tests := []struct {
input string
expected interface{}
}{
{`1_2.34`, 12.34},
{`12.3_4`, 12.34},
{`1_2.3_4`, 12.34},
{`1_2_3.4_5_6`, 123.456},
}

for i, tt := range tests {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
verifyExpected(t, i, evaluated, tt.expected)
v.checkCFP(t, i, 0)
v.checkSP(t, i, 1)
}
}
38 changes: 38 additions & 0 deletions vm/integer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,41 @@ func TestIntegerZeroDivisionFail(t *testing.T) {
v.checkSP(t, i, 1)
}
}

func TestIntegerWithWrongUnderscore(t *testing.T) {
testsFail := []errorTestCase{
{`1_`, "UndefinedMethodError: Undefined Method '_' for <Instance of: Object>", 1},
{`1__2`, "UndefinedMethodError: Undefined Method '__2' for <Instance of: Object>", 1},
{`_1`, "UndefinedMethodError: Undefined Method '_1' for <Instance of: Object>", 1},
}
for i, tt := range testsFail {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
checkErrorMsg(t, i, evaluated, tt.expected)
v.checkCFP(t, i, tt.expectedCFP)
v.checkSP(t, i, 1)
}

}

func TestIntegerWithCorrectUnderscore(t *testing.T) {

tests := []struct {
input string
expected interface{}
}{
{`1_234`, 1234},
{`1_2_34`, 1234},
{`1_23_4`, 1234},
{`1_2_3_4`, 1234},
}

for i, tt := range tests {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
verifyExpected(t, i, evaluated, tt.expected)
v.checkCFP(t, i, 0)
v.checkSP(t, i, 1)
}

}