Skip to content

Commit

Permalink
fix: token file output
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddie authored and Eddie committed Jun 19, 2024
1 parent 2cb5939 commit 60e8f45
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 38 deletions.
Binary file modified out/artifacts/jungle_jar/jungle.jar
Binary file not shown.
34 changes: 21 additions & 13 deletions programs/loop.ast
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
SEQUENCE
ASSIGN
IDENTIFIER i
LITERAL_INTEGER 3
SEQUENCE
SEQUENCE
ASSIGN
IDENTIFIER i
LITERAL_INTEGER 3
;
LOOP
OPERATOR_LESS_THAN
LITERAL_INTEGER 0
OPERATOR_GREATER_THAN
IDENTIFIER i
LITERAL_INTEGER 0
BLOCK
SEQUENCE
SEQUENCE
PRINT
IDENTIFIER i
;
PRINT
LITERAL_STRING \n
SEQUENCE
SEQUENCE
PRINT
IDENTIFIER i
;
;
PRINT
LITERAL_STRING ...\n
;
SLEEP
LITERAL_INTEGER 1000
;
ASSIGN
IDENTIFIER i
OPERATOR_SUBTRACT
IDENTIFIER i
LITERAL_INTEGER 1
;
PRINT
LITERAL_STRING Blast off!\n
;
PRINT
LITERAL_STRING Blast off!\n
;
2 changes: 1 addition & 1 deletion programs/loop.source
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
i = 3
loop (greaterThan i 0) {
print(i)
print('\n')
print('...\n')
sleep 1000
i = - i 1
}
Expand Down
46 changes: 26 additions & 20 deletions programs/loop.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,32 @@
4 2 SPACE
4 3 KEYWORD print
4 8 BRACKET_ROUND_OPEN
4 9 TEXT \\n
4 13 BRACKET_ROUND_CLOSE
4 14 NEWLINE
4 9 TEXT ...\\n
4 16 BRACKET_ROUND_CLOSE
4 17 NEWLINE
5 1 SPACE
5 2 SPACE
5 3 SYMBOL i
5 4 SPACE
5 5 EQUALS
5 6 SPACE
5 7 MINUS
5 3 KEYWORD sleep
5 8 SPACE
5 9 SYMBOL i
5 10 SPACE
5 11 NUMBER 1
5 12 NEWLINE
6 1 BRACKET_CURLY_CLOSE
6 2 NEWLINE
7 1 KEYWORD print
7 6 BRACKET_ROUND_OPEN
7 7 TEXT Blast off!\\n
7 21 BRACKET_ROUND_CLOSE
7 22 NEWLINE
7 23 TERMINAL
5 9 NUMBER 1000
5 13 NEWLINE
6 1 SPACE
6 2 SPACE
6 3 SYMBOL i
6 4 SPACE
6 5 EQUALS
6 6 SPACE
6 7 MINUS
6 8 SPACE
6 9 SYMBOL i
6 10 SPACE
6 11 NUMBER 1
6 12 NEWLINE
7 1 BRACKET_CURLY_CLOSE
7 2 NEWLINE
8 1 KEYWORD print
8 6 BRACKET_ROUND_OPEN
8 7 TEXT Blast off!\\n
8 21 BRACKET_ROUND_CLOSE
8 22 NEWLINE
8 23 TERMINAL
3 changes: 2 additions & 1 deletion src/main/java/com/jungle/ast/Node.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jungle.ast;

import com.jungle.common.StringUtils;
import com.jungle.error.LoadError;
import com.jungle.error.SaveError;
import com.jungle.logger.FileLogger;
Expand Down Expand Up @@ -103,7 +104,7 @@ public String getStringValue() {
if (getRawValue() == null) {
throw new Error("failed to parse value as string - value is null");
}
return getRawValue();
return StringUtils.unescapeString(getRawValue());
}

// endregion
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/jungle/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,8 @@ public INode parseTextLiteral() {
if (textValue == null) {
throw newError("text token missing value");
}
textValue = StringUtils.unescapeString(textValue);
// Note: a character literal cannot be empty, but a string literal can be empty
boolean isSingleCharacter = textValue.length() == 1;
boolean isSingleCharacter = StringUtils.unescapeString(textValue).length() == 1;
NodeType type = isSingleCharacter
? NodeType.LITERAL_CHARACTER
: NodeType.LITERAL_STRING;
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/jungle/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,9 @@ public void testParseTextLiteral_singleCharacterEscaped() {

assertNotNull(ast);
assertEquals(NodeType.LITERAL_CHARACTER, ast.getType());
assertEquals("\n", ast.getRawValue());
assertEquals("\\n", ast.getRawValue());
assertEquals("\n", ast.getStringValue());
assertEquals(new Character('\n'), ast.getCharacterValue());
}

@Test
Expand Down

0 comments on commit 60e8f45

Please sign in to comment.