Skip to content

Commit

Permalink
Fix a bug of tokenizing identifiers that contain numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
predatorray committed Sep 28, 2016
1 parent 80d5d30 commit 3ef86bf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/me/predatorray/bud/lisp/lexer/Lexer.java
Expand Up @@ -197,7 +197,7 @@ public Token next() {
if (isWhiteSpace(currChar)) {
return new Atmosphere(getCurrentCharLocation());
}
if (PREFIXES_OF_NUMBER.contains(currChar)) {
if (symbolValue.length() == 0 && PREFIXES_OF_NUMBER.contains(currChar)) {
numberValue.append(currChar);
state = TokenizerState.WITHIN_NUMBER;
numberLocation = location.clone();
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/me/predatorray/bud/lisp/BudInterpreterTest.java
Expand Up @@ -72,4 +72,9 @@ public void testDoubleNumberList() throws Exception {
new BudNumber(new BigDecimal(4)),
new BudNumber(new BigDecimal(6)))), "double-number-list.bud");
}

@Test
public void testFibonacci() throws Exception {
assertInterpretCorrectly(new BudNumber(new BigDecimal("573147844013817084101")), "fibonacci.bud");
}
}
22 changes: 17 additions & 5 deletions src/test/java/me/predatorray/bud/lisp/lexer/LexerTest.java
Expand Up @@ -21,7 +21,7 @@ private void assertMatches(List<? extends Token> expectedTokens, String input) {
}

@Test
public void testLexer1() {
public void testNormalApplication() {
assertMatches(Arrays.asList(
new LeftParenthesis(new TextLocation(1, 1)),
new IdentifierToken("+", new TextLocation(1, 2)),
Expand All @@ -34,7 +34,7 @@ public void testLexer1() {
}

@Test
public void testLexer2() {
public void testApplyingEscapedString() {
assertMatches(Arrays.asList(
new LeftParenthesis(new TextLocation(1, 1)),
new IdentifierToken("foobar", new TextLocation(1, 2)),
Expand All @@ -45,7 +45,7 @@ public void testLexer2() {
}

@Test
public void testLexer3() {
public void testMultipleIdentifiers() {
assertMatches(Arrays.asList(
new Atmosphere(new TextLocation(1, 1)),
new NumberToken(new BigDecimal(123), new TextLocation(2, 1)),
Expand All @@ -59,16 +59,28 @@ public void testLexer3() {
}

@Test
public void testLexer4() {
public void testBoolean() {
assertMatches(Arrays.asList(new BooleanToken(true, new TextLocation(1, 1)),
new Atmosphere(new TextLocation(1, 3)),
new BooleanToken(false, new TextLocation(1, 4))),
"#t #f");
}

@Test
public void testLexer5() {
public void testCharacter() {
assertMatches(Collections.singletonList(new CharacterToken((char) 1, new TextLocation(1, 1))),
"#\\001");
}

@Test
public void testNumber() {
assertMatches(Collections.singletonList(new NumberToken(new BigDecimal("0.123"), new TextLocation(1, 1))),
".123");
}

@Test
public void testIdentifierContainingNumber() {
assertMatches(Collections.singletonList(new IdentifierToken("fib0", new TextLocation(1, 1))),
"fib0");
}
}
7 changes: 7 additions & 0 deletions src/test/resources/me/predatorray/bud/lisp/fibonacci.bud
@@ -0,0 +1,7 @@
((lambda (x)
(define (fibonacci x a b)
(if (> x 0)
(fibonacci (- x 1) b (+ a b))
b))
(fibonacci x 0 1)
) 100)

0 comments on commit 3ef86bf

Please sign in to comment.