Browse files

Merge pull request #1 from fruchtose/master

Added one-line comments ("//") and multiline comments ("/*", "*/")
  • Loading branch information...
2 parents b196fe3 + c6c473a commit aa1cd2eb06d456c63b4bd96f0803279a4a1a5b70 @vgel committed Apr 6, 2012
Showing with 30 additions and 7 deletions.
  1. +2 −1 README.md
  2. +3 −2 in.tenc
  3. +24 −3 src/r194/Lexer.java
  4. +1 −1 src/r194/Parser.java
View
3 README.md
@@ -21,8 +21,9 @@ This code below:
return a(1) + i;
}
+ // This is a comment
int a(int a) {
- return a * 4;
+ return a * 4; /* Look, ma, no local variables! */
}
compiles to this assembly:
View
5 in.tenc
@@ -3,6 +3,7 @@ int main(){
return a(1) + i;
}
+// This is a comment
int a(int a) {
- return a * 4;
-}
+ return a * 4; /* Look, ma, no local variables! */
+}
View
27 src/r194/Lexer.java
@@ -7,7 +7,9 @@
enum Token {
NONE(null),
EOF(null),
- //COMMENT("\\/\\/.*?\n"),
+ COMMENT("//[^\n]*"),
+ OPEN_MULTILINE_COMMENT("/\\*"),
+ CLOSE_MULTILINE_COMMENT("\\*/"),
RETURN("return"),
WHITESPACE("\\p{Space}"),
TYPE_NAME("(int|void)"),
@@ -75,14 +77,33 @@ public String toString() {
List<Lexeme> ret = new ArrayList<>();
int start = 0;
int end = 0;
+ boolean commentState = false;
while (end < input.length()){
if (!isMatchable(input.substring(start, end + 1))){
String token = input.substring(start, end);
- if (isMatchable(token)){
+ if (isMatchable(token) || commentState){
Token type = Token.getMatching(token);
- ret.add(new Lexeme(token, type));
+
+ // Checks for open multi-line comment
+ if (type == Token.OPEN_MULTILINE_COMMENT) {
+ commentState = true;
+ }
+
+ if (!(commentState || type == Token.COMMENT)) {
+ ret.add(new Lexeme(token, type));
+ }
+
+ // Checks for end of multi-line comment
+ if (commentState) {
+ commentState = type != Token.CLOSE_MULTILINE_COMMENT;
+ }
+
start = end;
+
+ if (commentState) {
+ end++;
+ }
}
else {
end++;
View
2 src/r194/Parser.java
@@ -17,7 +17,7 @@ public void filterTokens(){
List<Lexeme> newTokens = new ArrayList<>();
for (int i = 0; i < tokens.size(); i++) {
Lexeme t = tokens.get(i);
- if (/*t.type != Token.COMMENT && */t.type != Token.WHITESPACE){
+ if (t.type != Token.WHITESPACE){
newTokens.add(t);
}
}

0 comments on commit aa1cd2e

Please sign in to comment.