Updated what defines a "word" to simplify logic a bit. Closes #10.

This commit is contained in:
Rob Greene 2018-05-19 10:39:47 -05:00
parent 573c0cdbe9
commit e208fa453c
4 changed files with 18 additions and 18 deletions

View File

@ -178,6 +178,8 @@ public enum ApplesoftKeyword {
tokenizer.resetSyntax();
tokenizer.wordChars('a', 'z');
tokenizer.wordChars('A', 'Z');
tokenizer.wordChars('$', '$'); // Experiment to pull in string marker
tokenizer.wordChars('%', '%'); // Experiment to pull in integer marker
tokenizer.wordChars(128 + 32, 255);
tokenizer.whitespaceChars(0, ' ');
tokenizer.quoteChar('"');

View File

@ -81,6 +81,7 @@ public class TokenReader {
return Optional.of(Token.number(line, tokenizer.nval));
case StreamTokenizer.TT_WORD:
Optional<ApplesoftKeyword> opt = ApplesoftKeyword.find(tokenizer.sval);
// REM is special
if (opt.filter(kw -> kw == ApplesoftKeyword.REM).isPresent()) {
StringBuilder sb = new StringBuilder();
while (true) {
@ -96,23 +97,24 @@ public class TokenReader {
}
return Optional.of(Token.comment(line, sb.toString()));
}
// If we found an Applesoft token, handle it special
// If we found an Applesoft token, handle it
if (opt.isPresent()) {
if (opt.get().parts.size() > 1) {
// Pull next token and see if it is the 2nd part ("MID$" == "MID", "$"; checking for the "$")
// Pull next token and see if it is the 2nd part ("PR#" == "PR", "#"; checking for the "#")
next(depth-1)
.filter(t -> opt.get().parts.get(1).equals(t.text))
.orElseThrow(() -> new IOException("Expecting: " + opt.get().parts));
}
return Optional.of(Token.keyword(line, opt.get()));
} else {
// Found an identifier. Need to find X, X%, X$, X(, X$(, X%( patterns.
// Found an identifier (A, A$, A%). Test if it is an array ('A(', 'A$(', 'A%(').
String sval = tokenizer.sval;
tokenizer.nextToken();
if (tokenizer.ttype == '%' || tokenizer.ttype == '$') {
if (tokenizer.ttype == '(') {
sval += (char)tokenizer.ttype;
} else {
tokenizer.pushBack();
}
tokenizer.pushBack();
return Optional.of(Token.ident(line, sval));
}
case '"':

View File

@ -3,14 +3,14 @@ package com.webcodepro.applecommander.util.applesoft;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeSet;
import java.util.function.Function;
import com.webcodepro.applecommander.util.applesoft.Token.Type;
@ -346,7 +346,7 @@ public class Visitors {
}
private static class VariableReportVisitor implements Visitor {
private Map<String,List<Integer>> refs = new HashMap<>();
private Map<String,SortedSet<Integer>> refs = new HashMap<>();
private int currentLineNumber = -1;
@Override
@ -357,15 +357,14 @@ public class Visitors {
.forEach(this::print);
return p;
}
private void print(Map.Entry<String,List<Integer>> e) {
private void print(Map.Entry<String,SortedSet<Integer>> e) {
System.out.printf("%-8s ", e.getKey());
int c = 0;
for (int i : e.getValue()) {
System.out.printf("%d, ", i);
if (c++ > 10) {
c = 0;
System.out.printf("\n ");
}
if (c > 0) System.out.print(", ");
if (c > 0 && c % 10 == 0) System.out.printf("\n ");
System.out.print(i);
c += 1;
}
System.out.println();
}
@ -380,7 +379,7 @@ public class Visitors {
public Token visit(Token token) {
if (token.type == Type.IDENT) {
refs.merge(token.text,
new ArrayList<>(Arrays.asList(currentLineNumber)),
new TreeSet<>(Arrays.asList(currentLineNumber)),
(a,b) -> { a.addAll(b); return a; });
}
return Visitor.super.visit(token);

View File

@ -1,5 +1,3 @@
TEXT
NEW
10 GOTO 100
20 REM DRAW CIRCLE ROUTINE
30 FOR A = 0 TO PT
@ -29,4 +27,3 @@ NEW
240 YO = (159 - SZ*2) * RND (1) + SZ
250 GOSUB 30
260 NEXT Q
RUN