2020-09-10 00:56:16 +00:00
|
|
|
|
|
|
|
/*************/
|
|
|
|
/*GEMWIRE */
|
|
|
|
/* ERYTHRO*/
|
|
|
|
/*************/
|
|
|
|
|
|
|
|
#include <Defs.h>
|
|
|
|
#include <Data.h>
|
|
|
|
|
|
|
|
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* * * * * * C H A R S T R E AM * * * * * *
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2021-01-20 19:22:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The Lexer holds a "stream" of characters.
|
|
|
|
* You may read a character from the stream, and if it is not
|
|
|
|
* the desired character, it may be placed into an "overread" buffer.
|
|
|
|
* The overread buffer is checked before the source file is read any further.
|
|
|
|
* This provides an effective way to "un-read" a character.
|
|
|
|
*
|
|
|
|
* @param Char: The character to "un-read"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
static void ReturnCharToStream(int Char) {
|
|
|
|
Overread = Char;
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:22:15 +00:00
|
|
|
/*
|
|
|
|
* NextChar allows you to ask the Lexer for the next useful character.
|
|
|
|
* As mentioned above, it checks the overread buffer first.
|
|
|
|
*
|
|
|
|
* @return the character as int
|
|
|
|
*
|
|
|
|
*/
|
2020-09-10 00:56:16 +00:00
|
|
|
static int NextChar(void) {
|
|
|
|
int Char;
|
|
|
|
|
2022-03-03 00:05:10 +00:00
|
|
|
if (Overread) {
|
2020-09-10 00:56:16 +00:00
|
|
|
Char = Overread;
|
|
|
|
Overread = 0;
|
|
|
|
return Char;
|
|
|
|
}
|
|
|
|
|
|
|
|
Char = fgetc(SourceFile);
|
|
|
|
|
2022-03-03 00:05:10 +00:00
|
|
|
if (Char == '\n')
|
2020-09-10 00:56:16 +00:00
|
|
|
Line++;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
return Char;
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:22:15 +00:00
|
|
|
/*
|
|
|
|
* Searches for the next useful character, skipping whitespace.
|
|
|
|
* @return the character as int.
|
|
|
|
*/
|
2020-09-10 00:56:16 +00:00
|
|
|
|
|
|
|
static int FindChar() {
|
|
|
|
int Char;
|
|
|
|
|
|
|
|
Char = NextChar();
|
|
|
|
|
2022-03-03 00:05:10 +00:00
|
|
|
while (Char == ' ' || Char == '\t' || Char == '\n' || Char == '\r') {
|
2020-09-10 00:56:16 +00:00
|
|
|
Char = NextChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Char;
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:22:15 +00:00
|
|
|
/*
|
|
|
|
* Allows the conversion between ASCII, hex and numerals.
|
|
|
|
* @param String: The set of all valid results
|
|
|
|
* @param Char: The ASCII character to convert
|
|
|
|
* @return the ASCII character in int form, if in the set of valid results. -1 if not.
|
|
|
|
*/
|
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
static int FindDigitFromPos(char* String, char Char) {
|
|
|
|
char* Result = strchr(String, Char);
|
2022-03-03 00:05:10 +00:00
|
|
|
return (Result ? Result - String : -1);
|
2020-09-10 00:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 19:22:15 +00:00
|
|
|
/*
|
|
|
|
* Facilitates the easy checking of expected tokens.
|
|
|
|
* NOTE: there is (soon to be) an optional variant of this function that
|
|
|
|
* reads a token but does not consume it ( via Tokenise )
|
|
|
|
*
|
|
|
|
* @param Type: The expected token, in terms of value of the TokenTypes enum.
|
|
|
|
* @param TokenExpected: A string to output when the token is not found.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
void VerifyToken(int Type, char* TokenExpected) {
|
2022-03-03 00:05:10 +00:00
|
|
|
if (CurrentToken.type == Type)
|
2021-01-20 19:22:15 +00:00
|
|
|
Tokenise();
|
2020-09-10 00:56:16 +00:00
|
|
|
else {
|
|
|
|
printf("Expected %s on line %d\n", TokenExpected, Line);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct Token* RejectedToken = NULL;
|
|
|
|
|
2021-01-20 19:22:15 +00:00
|
|
|
/*
|
|
|
|
* Rejected Tokens and the Overread Stream are identical concepts.
|
|
|
|
* This was implemented first, but it is no longer used.
|
|
|
|
* TODO: Refactor this function out.
|
|
|
|
*/
|
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
void RejectToken(struct Token* Token) {
|
2022-03-03 00:05:10 +00:00
|
|
|
if (RejectedToken != NULL)
|
2020-09-10 00:56:16 +00:00
|
|
|
Die("Cannot reject two tokens in a row!");
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
RejectedToken = Token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* * * * L I T E R A L S A N D I D E N T I F I E R S * * * *
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2021-01-20 19:22:15 +00:00
|
|
|
/*
|
|
|
|
* Facilitates the parsing of integer literals from the file.
|
|
|
|
* Currently only supports the decimal numbers, despite the
|
|
|
|
* FindDigitFromPos function allowing conversion.
|
|
|
|
*
|
|
|
|
* The functon loops over the characters, multiplying by 10 and adding
|
|
|
|
* the new value on top, until a non-numeric character is found.
|
|
|
|
* At that point, it returns the non-numeric character to the Overread Stream
|
|
|
|
* and returns the calculated number.
|
|
|
|
*
|
|
|
|
* @param Char: The first number to scan.
|
|
|
|
* @return the full parsed number as an int.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
static int ReadInteger(int Char) {
|
|
|
|
int CurrentChar = 0;
|
|
|
|
int IntegerValue = 0;
|
|
|
|
|
2022-03-03 00:05:10 +00:00
|
|
|
while ((CurrentChar = FindDigitFromPos("0123456789", Char)) >= 0) {
|
2020-09-10 00:56:16 +00:00
|
|
|
IntegerValue = IntegerValue * 10 + CurrentChar;
|
|
|
|
Char = NextChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnCharToStream(Char);
|
|
|
|
|
|
|
|
return IntegerValue;
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:22:15 +00:00
|
|
|
/*
|
|
|
|
* An Identifier can be any of:
|
|
|
|
* * A function name
|
|
|
|
* * A variable name
|
|
|
|
* * A struct name
|
|
|
|
* / A class name
|
|
|
|
* / An annotation name
|
|
|
|
*
|
|
|
|
* This function allows a full name to be read into a buffer, with a defined
|
|
|
|
* start character and a defined maximum text size limit.
|
|
|
|
*
|
|
|
|
* @param Char: The first char of the Identifier.
|
|
|
|
* @param Buffer: The location to store the Identifier. (usually CurrentIdentifer, a compiler global defined for this purpose)
|
|
|
|
* @param Limit: The maximum Identifer length.
|
|
|
|
* @return the length of the parsed identifier
|
|
|
|
*
|
|
|
|
*/
|
2020-09-10 00:56:16 +00:00
|
|
|
static int ReadIdentifier(int Char, char* Buffer, int Limit) {
|
2022-03-03 00:05:10 +00:00
|
|
|
int ind = 0;
|
2020-09-10 00:56:16 +00:00
|
|
|
|
|
|
|
// This defines the valid chars in a keyword/variable/function.
|
2022-03-03 00:05:10 +00:00
|
|
|
while (isalpha(Char) || isdigit(Char) || Char == '_') {
|
2020-09-10 00:56:16 +00:00
|
|
|
if (ind >= Limit - 1) {
|
|
|
|
printf("Identifier too long: %d\n", Line);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
Buffer[ind++] = Char;
|
|
|
|
}
|
|
|
|
|
|
|
|
Char = NextChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we've reached a non-keyword character
|
|
|
|
ReturnCharToStream(Char);
|
|
|
|
Buffer[ind] = '\0';
|
|
|
|
return ind;
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:22:15 +00:00
|
|
|
/*
|
|
|
|
* Char literals appear as 'x'
|
|
|
|
*
|
|
|
|
* They are bounded by two apostrophes.
|
|
|
|
* They can contain any 1-byte ASCII character, as well as some
|
|
|
|
* predefined, standard escape codes.
|
|
|
|
* This function attempts to get the character from the file, with escape codes intact.
|
|
|
|
*
|
|
|
|
* @return the character as an int
|
|
|
|
*
|
|
|
|
*/
|
2020-11-22 00:41:48 +00:00
|
|
|
static int ReadCharLiteral() {
|
|
|
|
int Char;
|
|
|
|
Char = NextChar();
|
2022-03-03 00:05:10 +00:00
|
|
|
if (Char == '\\') {
|
|
|
|
switch (Char = NextChar()) {
|
|
|
|
case 'a':
|
|
|
|
return '\a';
|
|
|
|
case 'b':
|
|
|
|
return '\b';
|
|
|
|
case 'f':
|
|
|
|
return '\f';
|
|
|
|
case 'n':
|
|
|
|
return '\n';
|
|
|
|
case 'r':
|
|
|
|
return '\r';
|
|
|
|
case 't':
|
|
|
|
return '\t';
|
|
|
|
case 'v':
|
|
|
|
return '\v';
|
|
|
|
case '\\':
|
|
|
|
return '\\';
|
|
|
|
case '"':
|
|
|
|
return '"';
|
|
|
|
case '\'':
|
|
|
|
return '\'';
|
2020-11-22 00:41:48 +00:00
|
|
|
default:
|
2020-11-22 00:43:32 +00:00
|
|
|
DieChar("Unknown Escape: ", Char);
|
2020-11-22 00:41:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Char;
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:22:15 +00:00
|
|
|
/*
|
|
|
|
* String literals appear as "hello world"
|
|
|
|
*
|
|
|
|
* They are bounded by two quotation marks.
|
|
|
|
* They can contain an arbitrary length of text.
|
|
|
|
* They are backed by an array of chars (hence the char* type) and thus
|
|
|
|
* have a practically unlimited length.
|
|
|
|
*
|
|
|
|
* To read a String Literal, it is a simple matter of reading Char Literals until
|
|
|
|
* the String termination token is identified - the last quotation mark.
|
|
|
|
*
|
|
|
|
* @param Buffer: The buffer into which to write the string. (usually CurrentIdentifer, a compiler global defined for this purpose)
|
|
|
|
*
|
|
|
|
*/
|
2020-11-22 01:44:54 +00:00
|
|
|
static int ReadStringLiteral(char* Buffer) {
|
|
|
|
int Char;
|
|
|
|
|
2022-03-03 00:05:10 +00:00
|
|
|
for (int i = 0; i < TEXTLEN - 1; i++) {
|
|
|
|
if ((Char = ReadCharLiteral()) == '"') {
|
|
|
|
Buffer[i] = 0;
|
|
|
|
return i;
|
2020-11-22 01:44:54 +00:00
|
|
|
}
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-11-22 01:44:54 +00:00
|
|
|
Buffer[i] = Char;
|
|
|
|
}
|
|
|
|
|
|
|
|
Die("String Literal Too Long");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
/*
|
2021-01-20 19:22:15 +00:00
|
|
|
* Keywords are source-code tokens / strings that are reserved for the compiler.
|
|
|
|
* They cannot be used as identifers on their own.
|
|
|
|
*
|
|
|
|
* This function is where all of the keywords are added, and where most aliases are going to be stored.
|
|
|
|
*
|
|
|
|
* It uses a switch on the first character of the input string as an optimisation - rather than checking each
|
|
|
|
* keyword against the String individually, it only needs to compare a single number. This can be optimised into
|
|
|
|
* a hash table by the compiler for further optimisation, making this one of the fastest ways to switch
|
|
|
|
* on a full string.
|
|
|
|
*
|
|
|
|
* @param Str: The keyword input to try to parse
|
|
|
|
* @return the token expressed in terms of values of the TokenTypes enum
|
2020-09-10 00:56:16 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static int ReadKeyword(char* Str) {
|
|
|
|
// First, scan with reference intact.
|
2022-03-03 00:05:10 +00:00
|
|
|
switch (*Str) {
|
2020-09-10 00:56:16 +00:00
|
|
|
// This lets us case against the first char:
|
|
|
|
case ':':
|
2022-03-03 00:05:10 +00:00
|
|
|
if (!strcmp(Str, "::"))
|
2020-09-10 00:56:16 +00:00
|
|
|
return KW_FUNC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
2022-03-03 00:05:10 +00:00
|
|
|
if (!strcmp(Str, "char"))
|
2020-09-10 00:56:16 +00:00
|
|
|
return TY_CHAR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e':
|
2022-03-03 00:05:10 +00:00
|
|
|
if (!strcmp(Str, "else"))
|
2020-09-10 00:56:16 +00:00
|
|
|
return KW_ELSE;
|
|
|
|
|
|
|
|
break;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
case 'f':
|
2022-03-03 00:05:10 +00:00
|
|
|
if (!strcmp(Str, "for"))
|
2020-09-10 00:56:16 +00:00
|
|
|
return KW_FOR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
2021-01-17 06:36:11 +00:00
|
|
|
// alias char, int and long types
|
|
|
|
|
2022-03-03 00:05:10 +00:00
|
|
|
if (!strcmp(Str, "i8"))
|
2021-01-17 06:36:11 +00:00
|
|
|
return TY_CHAR;
|
2022-03-03 00:05:10 +00:00
|
|
|
if (!strcmp(Str, "i32"))
|
2021-01-17 06:36:11 +00:00
|
|
|
return TY_INT;
|
2022-03-03 00:05:10 +00:00
|
|
|
if (!strcmp(Str, "i64"))
|
2021-01-17 06:36:11 +00:00
|
|
|
return TY_LONG;
|
2020-09-10 00:56:16 +00:00
|
|
|
|
2022-03-03 00:05:10 +00:00
|
|
|
if (!strcmp(Str, "int"))
|
2020-09-10 00:56:16 +00:00
|
|
|
return TY_INT;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
|
|
|
if (!strcmp(Str, "if"))
|
2020-09-10 00:56:16 +00:00
|
|
|
return KW_IF;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
2022-03-03 00:05:10 +00:00
|
|
|
if (!strcmp(Str, "long"))
|
2020-09-10 00:56:16 +00:00
|
|
|
return TY_LONG;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
2022-03-03 00:05:10 +00:00
|
|
|
if (!strcmp(Str, "print"))
|
2020-09-10 00:56:16 +00:00
|
|
|
return KW_PRINT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
2022-03-03 00:05:10 +00:00
|
|
|
if (!strcmp(Str, "return"))
|
2020-09-10 00:56:16 +00:00
|
|
|
return KW_RETURN;
|
|
|
|
break;
|
2021-01-20 19:22:15 +00:00
|
|
|
|
|
|
|
case 's':
|
2022-03-03 00:05:10 +00:00
|
|
|
if (!strcmp(Str, "struct"))
|
2021-01-20 19:22:15 +00:00
|
|
|
return KW_STRUCT;
|
|
|
|
break;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
case 'v':
|
2022-03-03 00:05:10 +00:00
|
|
|
if (!strcmp(Str, "void"))
|
2020-09-10 00:56:16 +00:00
|
|
|
return TY_VOID;
|
|
|
|
break;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
case 'w':
|
2022-03-03 00:05:10 +00:00
|
|
|
if (!strcmp(Str, "while"))
|
2020-09-10 00:56:16 +00:00
|
|
|
return KW_WHILE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* * * * T O K E N I S E R * * * *
|
|
|
|
* * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2021-01-20 19:22:15 +00:00
|
|
|
/*
|
|
|
|
* Handles the majority of the work of reading tokens into the stream.
|
|
|
|
* It reads chars with FindChar, categorizing individual characters or small
|
|
|
|
* strings into their proper expression (as a value of the TokenTypes enum)
|
|
|
|
*
|
|
|
|
* It also defers the reading of numeric literals and char literals to the proper functions.
|
|
|
|
*
|
|
|
|
* If needed, it can also read Identifiers, for variable or function naming.
|
|
|
|
*
|
|
|
|
* This function may be the main bottleneck in the lexer.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void Tokenise() {
|
2020-09-10 00:56:16 +00:00
|
|
|
int Char, TokenType;
|
2021-01-20 19:22:15 +00:00
|
|
|
struct Token* Token = &CurrentToken;
|
2020-09-10 00:56:16 +00:00
|
|
|
|
2022-03-03 00:05:10 +00:00
|
|
|
if (RejectedToken != NULL) {
|
2020-09-10 00:56:16 +00:00
|
|
|
Token = RejectedToken;
|
|
|
|
RejectedToken = NULL;
|
2021-01-22 01:01:53 +00:00
|
|
|
return;
|
2020-09-10 00:56:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Char = FindChar();
|
|
|
|
|
2022-03-03 00:05:10 +00:00
|
|
|
switch (Char) {
|
2020-09-10 00:56:16 +00:00
|
|
|
case EOF:
|
|
|
|
Token->type = LI_EOF;
|
2021-01-22 01:01:53 +00:00
|
|
|
return;
|
2020-09-10 00:56:16 +00:00
|
|
|
|
2021-07-04 23:07:04 +00:00
|
|
|
case '.':
|
|
|
|
Token->type = LI_DOT;
|
|
|
|
return;
|
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
case '+':
|
2020-11-23 21:42:32 +00:00
|
|
|
// + can be either "+" or "++".
|
|
|
|
Char = NextChar();
|
2022-03-03 00:05:10 +00:00
|
|
|
if (Char == '+') {
|
2020-11-23 21:42:32 +00:00
|
|
|
Token->type = PPMM_PLUS;
|
|
|
|
} else {
|
|
|
|
Token->type = AR_PLUS;
|
|
|
|
ReturnCharToStream(Char);
|
|
|
|
}
|
2020-09-10 00:56:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
2021-07-04 23:07:04 +00:00
|
|
|
// - can be either "-" or "--" or "->"
|
2020-11-23 21:42:32 +00:00
|
|
|
Char = NextChar();
|
2022-03-03 00:05:10 +00:00
|
|
|
if (Char == '-') {
|
2020-11-23 21:42:32 +00:00
|
|
|
Token->type = PPMM_MINUS;
|
2022-03-03 00:05:10 +00:00
|
|
|
} else if (Char == '>') {
|
2021-07-04 23:07:04 +00:00
|
|
|
Token->type = LI_ARROW;
|
2020-11-23 21:42:32 +00:00
|
|
|
} else {
|
|
|
|
Token->type = AR_MINUS;
|
|
|
|
ReturnCharToStream(Char);
|
|
|
|
}
|
2020-09-10 00:56:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '*':
|
|
|
|
Token->type = AR_STAR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '/':
|
|
|
|
Token->type = AR_SLASH;
|
|
|
|
break;
|
2020-09-13 01:26:49 +00:00
|
|
|
|
|
|
|
case '&':
|
2020-11-23 21:42:32 +00:00
|
|
|
Char = NextChar();
|
2022-03-03 00:05:10 +00:00
|
|
|
if (Char == '&') {
|
2020-11-23 21:42:32 +00:00
|
|
|
Token->type = BOOL_AND;
|
|
|
|
} else {
|
|
|
|
Token->type = BIT_AND;
|
|
|
|
ReturnCharToStream(Char);
|
|
|
|
}
|
|
|
|
break;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-11-23 21:42:32 +00:00
|
|
|
case '|':
|
|
|
|
Char = NextChar();
|
2022-03-03 00:05:10 +00:00
|
|
|
if (Char == '|') {
|
2020-11-23 21:42:32 +00:00
|
|
|
Token->type = BOOL_OR;
|
|
|
|
} else {
|
|
|
|
Token->type = BIT_OR;
|
|
|
|
ReturnCharToStream(Char);
|
|
|
|
}
|
|
|
|
break;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-11-23 21:42:32 +00:00
|
|
|
case '^':
|
|
|
|
Token->type = BIT_XOR;
|
2020-09-13 01:26:49 +00:00
|
|
|
break;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-11-23 21:42:32 +00:00
|
|
|
case '~':
|
|
|
|
Token->type = BIT_NOT;
|
|
|
|
break;
|
|
|
|
|
2020-09-13 22:41:46 +00:00
|
|
|
case ',':
|
|
|
|
Token->type = LI_COM;
|
|
|
|
break;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
case '=':
|
|
|
|
Char = NextChar();
|
|
|
|
// If the next char is =, we have ==, the compare equality token.
|
2022-03-03 00:05:10 +00:00
|
|
|
if (Char == '?') {
|
2020-09-10 00:56:16 +00:00
|
|
|
Token->type = CMP_EQUAL;
|
2022-03-03 00:05:10 +00:00
|
|
|
// if the next char is >, we have =>, the greater than or equal token.
|
|
|
|
} else if (Char == '>') {
|
2020-09-10 00:56:16 +00:00
|
|
|
Token->type = CMP_GTE;
|
2022-03-03 00:05:10 +00:00
|
|
|
// If none of the above match, we have = and an extra char. Return the char and set the token
|
2020-09-10 00:56:16 +00:00
|
|
|
} else {
|
|
|
|
ReturnCharToStream(Char);
|
|
|
|
Token->type = LI_EQUAL;
|
|
|
|
}
|
|
|
|
break;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
case '!':
|
|
|
|
Char = NextChar();
|
|
|
|
// If the next char is =, we have !=, the compare inequality operator.
|
2022-03-03 00:05:10 +00:00
|
|
|
if (Char == '=') {
|
2020-09-10 00:56:16 +00:00
|
|
|
Token->type = CMP_INEQ;
|
2022-03-03 00:05:10 +00:00
|
|
|
// Otherwise, we have a spare char
|
2020-09-10 00:56:16 +00:00
|
|
|
} else {
|
2020-11-23 21:42:32 +00:00
|
|
|
Token->type = BOOL_INVERT;
|
2022-03-03 00:05:10 +00:00
|
|
|
ReturnCharToStream(Char);
|
2020-09-10 00:56:16 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '<':
|
|
|
|
Char = NextChar();
|
|
|
|
// If the next char is =, we have <=, the less than or equal comparator.
|
2022-03-03 00:05:10 +00:00
|
|
|
if (Char == '=') {
|
2020-09-10 00:56:16 +00:00
|
|
|
Token->type = CMP_LTE;
|
2022-03-03 00:05:10 +00:00
|
|
|
} else if (Char == '<') { // But if the next char is <, we have << - the Shift Left operator.
|
2020-11-23 21:42:32 +00:00
|
|
|
Token->type = SH_LEFT;
|
2020-09-10 00:56:16 +00:00
|
|
|
} else {
|
|
|
|
ReturnCharToStream(Char);
|
|
|
|
Token->type = CMP_LT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '>':
|
2020-11-23 21:42:32 +00:00
|
|
|
// For >, Less than or equal is => so we can ignore it, but the Shift Right operator is >>.
|
|
|
|
Char = NextChar();
|
2022-03-03 00:05:10 +00:00
|
|
|
if (Char == '>') {
|
2020-11-23 21:42:32 +00:00
|
|
|
Token->type = SH_RIGHT;
|
|
|
|
} else {
|
|
|
|
Token->type = CMP_GT;
|
|
|
|
ReturnCharToStream(Char);
|
|
|
|
}
|
2020-09-10 00:56:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ';':
|
|
|
|
Token->type = LI_SEMIC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
Token->type = LI_LPARE;
|
|
|
|
break;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
case ')':
|
|
|
|
Token->type = LI_RPARE;
|
|
|
|
break;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
case '{':
|
|
|
|
Token->type = LI_LBRAC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '}':
|
|
|
|
Token->type = LI_RBRAC;
|
|
|
|
break;
|
|
|
|
|
2020-11-18 20:49:08 +00:00
|
|
|
case '[':
|
|
|
|
Token->type = LI_LBRAS;
|
|
|
|
break;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-11-18 20:49:08 +00:00
|
|
|
case ']':
|
|
|
|
Token->type = LI_RBRAS;
|
|
|
|
break;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
case ':':
|
|
|
|
Char = NextChar();
|
|
|
|
|
2022-03-03 00:05:10 +00:00
|
|
|
if (Char == ':') {
|
2020-09-10 00:56:16 +00:00
|
|
|
Token->type = KW_FUNC;
|
|
|
|
} else {
|
|
|
|
ReturnCharToStream(Char);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-11-22 00:41:48 +00:00
|
|
|
case '\'':
|
|
|
|
Token->value = ReadCharLiteral();
|
|
|
|
Token->type = LI_INT;
|
|
|
|
|
2022-03-03 00:05:10 +00:00
|
|
|
if (NextChar() != '\'')
|
2020-11-22 00:41:48 +00:00
|
|
|
Die("Expected '\\'' at the end of a character.");
|
|
|
|
break;
|
|
|
|
|
2020-11-22 01:44:54 +00:00
|
|
|
case '"':
|
|
|
|
ReadStringLiteral(CurrentIdentifier);
|
|
|
|
Token->type = LI_STR;
|
|
|
|
break;
|
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
default:
|
2022-03-03 00:05:10 +00:00
|
|
|
if (isdigit(Char)) {
|
2020-09-10 00:56:16 +00:00
|
|
|
|
|
|
|
Token->value = ReadInteger(Char);
|
|
|
|
Token->type = LI_INT;
|
|
|
|
break;
|
2022-03-03 00:05:10 +00:00
|
|
|
|
|
|
|
} else if (isalpha(Char) ||
|
|
|
|
Char == '_') { // This is what defines what a variable/function/keyword can START with.
|
2020-09-10 00:56:16 +00:00
|
|
|
ReadIdentifier(Char, CurrentIdentifier, TEXTLEN);
|
|
|
|
|
2022-03-03 00:05:10 +00:00
|
|
|
if (TokenType = ReadKeyword(CurrentIdentifier)) {
|
2020-09-10 00:56:16 +00:00
|
|
|
Token->type = TokenType;
|
|
|
|
break;
|
|
|
|
}
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
Token->type = TY_IDENTIFIER;
|
|
|
|
break;
|
2022-03-03 00:05:10 +00:00
|
|
|
//printf.er("Line %d: Unrecognized symbol %s\n", CurrentIdentifier, Line);
|
2020-09-10 00:56:16 +00:00
|
|
|
//exit(1);
|
|
|
|
}
|
|
|
|
|
2022-03-03 00:05:10 +00:00
|
|
|
|
2020-09-10 00:56:16 +00:00
|
|
|
DieChar("Unrecognized character", Char);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|