Skip to content

Commit

Permalink
Add parsing of numbers to tokeniser in VxBuild reimpl
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Livesey committed Aug 13, 2024
1 parent 5250d58 commit 46a506b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/test.vxl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
'test2'
"test2" 123.56e+7
2 changes: 1 addition & 1 deletion stdlib/io/io.vxl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function readFile(path, start, end) {
var fileSize = syscall io_seek(file, -1, false);

if (end >= fileSize) {
end = fileSize - 1;
end = fileSize;
}

var bytesToRead = end - start;
Expand Down
4 changes: 2 additions & 2 deletions test/stdlib-io/expected.log
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Hello, world!
File size: 20
File contents: This is a test file
Written file contents: This is a test
File contents: This is a test file.
Written file contents: This is a test!
47 changes: 43 additions & 4 deletions tools/vxbuild/tokeniser.vxl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function tokenise(sourceContainer) {

var mutables = {
match: null,
matchedString: null,
source: sourceContainer.source,
previousPosition: 0,
currentPosition: 0
Expand All @@ -80,6 +81,7 @@ export function tokenise(sourceContainer) {
var match = mutables.match = patterns.match(mutables.source, pattern, {matchRest: true});

if (match.matches) {
mutables.matchedString = mutables.source.substring(0, match.index);
mutables.source = mutables.source.substring(match.index);
mutables.previousPosition = mutables.currentPosition;
mutables.currentPosition += match.index;
Expand All @@ -91,7 +93,7 @@ export function tokenise(sourceContainer) {
}

function addToken(tokenClass, value) {
value ??= mutables.source.substring(0, mutables.match.index);
value ??= mutables.matchedString;

var token = new tokenClass(value);

Expand Down Expand Up @@ -149,7 +151,7 @@ export function tokenise(sourceContainer) {
continue;
}

if (matchToken("\*")) {
if (matchToken("/*")) {
blockCommentDepth++;
continue;
}
Expand All @@ -161,7 +163,7 @@ export function tokenise(sourceContainer) {
}

matchToken([
any([
patterns.any([
patterns.repeat(1, infinity, [
patterns.anyCharExcept("*/")
]),
Expand Down Expand Up @@ -336,7 +338,44 @@ export function tokenise(sourceContainer) {
continue;
}

// TODO: Add all other syntax features
if (matchToken([
patterns.any([patterns.ALPHA, patterns.anyChar("$_")]),
patterns.repeat(0, infinity, [
patterns.any([patterns.ALPHANUMERIC, patterns.anyChar("$_")])
])
])) {
addToken(IdentifierToken);
continue;
}

if (matchToken([
patterns.any([
patterns.pattern([
patterns.repeat(1, infinity, [patterns.DIGIT]),
patterns.maybe([
patterns.string("."),
patterns.repeat(0, infinity, [patterns.DIGIT])
])
]),
patterns.pattern([
patterns.repeat(0, infinity, [patterns.DIGIT]),
patterns.string("."),
patterns.maybe([
patterns.repeat(1, infinity, [patterns.DIGIT])
])
])
]),
patterns.maybe([
patterns.anyChar("eE"),
patterns.maybe([patterns.anyChar("+-")]),
patterns.repeat(1, infinity, [patterns.DIGIT])
])
])) {
addToken(NumberToken, mutables.matchedString.toNumber());
continue;
}

// TODO: Allow base conversions in Voxel to parse binary, octal and hexadecimal numbers

if (matchToken([patterns.WHITESPACE_STRING])) {
continue;
Expand Down

0 comments on commit 46a506b

Please sign in to comment.