2005-05-01 11:48:36 +00:00
|
|
|
#include "Lexer.h"
|
|
|
|
#include "Number.h"
|
|
|
|
#include "string.h"
|
2005-05-08 10:14:27 +00:00
|
|
|
#include "Boolean.h"
|
|
|
|
#include "Error.h"
|
2005-05-02 04:58:22 +00:00
|
|
|
#include "defs.h"
|
2005-05-01 11:48:36 +00:00
|
|
|
|
|
|
|
BOOL isdigit (string x)
|
|
|
|
{
|
|
|
|
return (x == "0" || x == "1" || x == "2" || x == "3" ||
|
|
|
|
x == "4" || x == "5" || x == "6" || x == "7" ||
|
|
|
|
x == "8" || x == "9");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL isspace (string x)
|
|
|
|
{
|
|
|
|
return (x == " " || x == "\t" || x == "\n" || x == "\r");
|
|
|
|
}
|
|
|
|
|
2005-05-08 21:51:16 +00:00
|
|
|
BOOL isjunk (string x)
|
|
|
|
{
|
|
|
|
return isspace (x) || x == ";";
|
|
|
|
}
|
|
|
|
|
2005-05-01 11:48:36 +00:00
|
|
|
BOOL issymbol (string x)
|
|
|
|
{
|
2005-05-08 21:51:16 +00:00
|
|
|
return (x != "" && x != "(" && x !=")" && !isjunk (x));
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@implementation Lexer
|
|
|
|
|
2005-05-02 04:58:22 +00:00
|
|
|
+ (id) newFromSource: (string) s file: (string) f
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
2005-05-02 04:58:22 +00:00
|
|
|
return [[Lexer alloc] initWithSource: s file: f];
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 04:58:22 +00:00
|
|
|
- (id) initWithSource: (string) s file: (string) f
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
2005-05-02 02:33:44 +00:00
|
|
|
self = [super init];
|
2005-05-06 23:25:06 +00:00
|
|
|
source = str_new();
|
|
|
|
str_copy(source, s);
|
2005-05-02 04:58:22 +00:00
|
|
|
filename = f;
|
|
|
|
linenum = 1;
|
2005-05-02 02:33:44 +00:00
|
|
|
return self;
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (SchemeObject) nextToken
|
|
|
|
{
|
|
|
|
local integer len;
|
|
|
|
local Number num;
|
|
|
|
local Symbol sym;
|
|
|
|
local String str;
|
2005-05-08 10:14:27 +00:00
|
|
|
local Boolean bl;
|
2005-05-02 04:58:22 +00:00
|
|
|
|
2005-05-08 21:51:16 +00:00
|
|
|
for (len = 0; isjunk(str_mid(source, len, len+1)); len++) {
|
|
|
|
if (str_mid(source, len, len+1) == ";") {
|
|
|
|
while (str_mid(source, len, len+1) != "\n") {
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
2005-05-02 04:58:22 +00:00
|
|
|
if (str_mid(source, len, len+1) == "\n") {
|
|
|
|
linenum++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-06 23:25:06 +00:00
|
|
|
str_copy (source, str_mid(source, len));
|
2005-05-01 11:48:36 +00:00
|
|
|
|
|
|
|
switch (str_mid (source, 0, 1)) {
|
|
|
|
case "(":
|
2005-05-06 23:25:06 +00:00
|
|
|
str_copy (source, str_mid (source, 1));
|
2005-05-01 11:48:36 +00:00
|
|
|
return [Symbol leftParen];
|
|
|
|
case ")":
|
2005-05-06 23:25:06 +00:00
|
|
|
str_copy (source, str_mid (source, 1));
|
2005-05-01 11:48:36 +00:00
|
|
|
return [Symbol rightParen];
|
|
|
|
case "0": case "1": case "2":
|
|
|
|
case "3": case "4": case "5":
|
|
|
|
case "6": case "7": case "8":
|
|
|
|
case "9":
|
|
|
|
for (len = 1; isdigit(str_mid(source, len, len+1)); len++);
|
|
|
|
num = [Number newFromInt: stoi (str_mid(source, 0, len))];
|
2005-05-02 04:58:22 +00:00
|
|
|
[num source: filename];
|
|
|
|
[num line: linenum];
|
2005-05-06 23:25:06 +00:00
|
|
|
str_copy (source, str_mid(source, len));
|
2005-05-01 11:48:36 +00:00
|
|
|
return num;
|
|
|
|
case "\"":
|
|
|
|
for (len = 1; str_mid(source, len, len+1) != "\""; len++);
|
|
|
|
str = [String newFromString: str_mid(source, 1, len)];
|
2005-05-02 04:58:22 +00:00
|
|
|
[str source: filename];
|
|
|
|
[str line: linenum];
|
2005-05-06 23:25:06 +00:00
|
|
|
str_copy (source, str_mid(source, len+1));
|
2005-05-01 11:48:36 +00:00
|
|
|
return str;
|
|
|
|
case "'":
|
2005-05-06 23:25:06 +00:00
|
|
|
str_copy (source, str_mid (source, 1));
|
2005-05-01 11:48:36 +00:00
|
|
|
return [Symbol quote];
|
2005-05-06 23:25:06 +00:00
|
|
|
case ".":
|
|
|
|
str_copy (source, str_mid (source, 1));
|
|
|
|
return [Symbol dot];
|
2005-05-08 10:14:27 +00:00
|
|
|
case "#":
|
|
|
|
str_copy (source, str_mid (source, 1));
|
|
|
|
switch (str_mid (source, 0, 1)) {
|
|
|
|
case "t":
|
|
|
|
bl = [Boolean trueConstant];
|
|
|
|
str_copy (source, str_mid (source, 1));
|
|
|
|
return bl;
|
|
|
|
case "f":
|
|
|
|
bl = [Boolean falseConstant];
|
|
|
|
str_copy (source, str_mid (source, 1));
|
|
|
|
return bl;
|
|
|
|
default:
|
|
|
|
return [Error type: "parse"
|
|
|
|
message: "Invalid # constant"
|
|
|
|
by: self];
|
|
|
|
}
|
2005-05-01 11:48:36 +00:00
|
|
|
case "":
|
|
|
|
return NIL;
|
|
|
|
default:
|
|
|
|
for (len = 1; issymbol(str_mid(source, len, len+1)); len++);
|
2005-05-02 02:33:44 +00:00
|
|
|
sym = [Symbol forString: str_mid (source, 0, len)];
|
2005-05-06 23:25:06 +00:00
|
|
|
str_copy (source, str_mid(source, len));
|
2005-05-01 11:48:36 +00:00
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-02 04:58:22 +00:00
|
|
|
- (integer) lineNumber
|
|
|
|
{
|
|
|
|
return linenum;
|
|
|
|
}
|
|
|
|
|
2005-05-08 10:14:27 +00:00
|
|
|
- (integer) line
|
|
|
|
{
|
|
|
|
return linenum;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (string) source
|
|
|
|
{
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2005-05-01 11:48:36 +00:00
|
|
|
@end
|