-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2af613
commit 47a2b38
Showing
16 changed files
with
283 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"name": "Mac", | ||
"includePath": [ | ||
"${workspaceFolder}/**" | ||
], | ||
"defines": [], | ||
"macFrameworkPath": [ | ||
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks" | ||
], | ||
"compilerPath": "/usr/bin/clang", | ||
"cStandard": "c17", | ||
"cppStandard": "c++17", | ||
"intelliSenseMode": "macos-clang-arm64" | ||
} | ||
], | ||
"version": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{ | ||
"files.associations": { | ||
"string": "cpp" | ||
"string": "cpp", | ||
"token.h": "c", | ||
"token_type.h": "c" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
all: output execute | ||
|
||
output: | ||
gcc src/main.c -o build/main | ||
gcc src/main.c src/utils/ints.c src/utils/strings.c src/lexer/lexer.c -o build/main | ||
|
||
execute: | ||
./build/main |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef COMMON_TYPES_H | ||
#define COMMON_TYPES_H | ||
|
||
typedef char* string; | ||
|
||
#endif // TCOMMON_YPES_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,134 @@ | ||
#ifndef LEXER_C | ||
#define LEXER_C | ||
|
||
#include "token.h" | ||
#include "token_type.h" | ||
#include "types.h" | ||
#include "token/token.h" | ||
#include "token/token_type.h" | ||
|
||
#include "../common/types.h" | ||
#include "../utils/strings.h" | ||
#include "../utils/ints.h" | ||
|
||
#include <string.h> | ||
#include <stdio.h> | ||
|
||
/** | ||
* @brief Pushes a token to the back of a token array. | ||
* | ||
* @param tokens The token array. | ||
* @param token The token to push. | ||
*/ | ||
void push_back(Token tokens[], Token token) | ||
{ | ||
tokens[sizeof(tokens) + 1] = token; | ||
} | ||
|
||
/** | ||
* @brief Pops a token from the front of a token array. | ||
* | ||
* @param tokens The token array. | ||
* @return Token The popped token. | ||
*/ | ||
string pop(string *tokens) | ||
{ | ||
string res = tokens[0]; | ||
for (int i = 1; i < sizeof(tokens); i++) | ||
{ | ||
tokens[i - 1] = tokens[i]; | ||
} | ||
return res; | ||
} | ||
|
||
/** | ||
* @brief Tokenizes a string. | ||
* | ||
* @param src The string to tokenize. | ||
* @return const Token* The tokens. | ||
*/ | ||
Token *tokenize(string src) | ||
{ | ||
// The list of tokens | ||
Token tokens[] = {}; | ||
|
||
// Split the src | ||
string *split_src = split(src, " "); | ||
|
||
// Build each token | ||
while (sizeof(split_src) > 0) | ||
{ | ||
// Pop the token | ||
string token = pop(split_src); | ||
|
||
// Check the token type | ||
if (token == ' ' || token == '\n' || token == '\t') | ||
{ | ||
continue; | ||
} | ||
else if (token == '(') | ||
{ | ||
push_back(tokens, (Token){TOKEN_TYPE_LEFT_PAREN, "("}); | ||
} | ||
else if (token == ')') | ||
{ | ||
push_back(tokens, (Token){TOKEN_TYPE_RIGHT_PAREN, ")"}); | ||
} | ||
else if (token == '=') | ||
{ | ||
push_back(tokens, (Token){TOKEN_TYPE_EQUAL, "="}); | ||
} | ||
else if (token == '+') | ||
{ | ||
push_back(tokens, (Token){TOKEN_TYPE_PLUS, "+"}); | ||
} | ||
else if (token == '-') | ||
{ | ||
push_back(tokens, (Token){TOKEN_TYPE_MINUS, "-"}); | ||
} | ||
else if (token == '*') | ||
{ | ||
push_back(tokens, (Token){TOKEN_TYPE_MULTIPLY, "*"}); | ||
} | ||
else if (token == '/') | ||
{ | ||
Token token = {TOKEN_TYPE_DIVIDE, "/"}; | ||
push_back(tokens, token); | ||
} | ||
else | ||
{ | ||
if (is_int(token)) | ||
{ | ||
string num = ""; | ||
while (sizeof(split_src) > 0 && is_int(split_src[0])) | ||
{ | ||
string n = pop(split_src); | ||
num = strcat(num, n); | ||
} | ||
push_back(tokens, (Token){TOKEN_TYPE_NUMBER, num}); | ||
} | ||
else if (is_alpha(token)) | ||
{ | ||
string str = ""; | ||
while (sizeof(split_src) > 0 && is_alpha(split_src[0])) | ||
{ | ||
string n = pop(split_src); | ||
str = strcat(str, n); | ||
} | ||
if (token == "let") | ||
{ | ||
push_back(tokens, (Token){TOKEN_TYPE_LET, "let"}); | ||
} | ||
else | ||
{ | ||
push_back(tokens, (Token){TOKEN_TYPE_IDENTIFIER, str}); | ||
} | ||
} | ||
else | ||
{ | ||
return tokens; | ||
} | ||
} | ||
} | ||
|
||
return tokens; | ||
} | ||
|
||
#endif // LEXER_C |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
#ifndef LEXER_h | ||
#define LEXER_h | ||
#ifndef LEXER_H | ||
#define LEXER_H | ||
|
||
#include "token/token.h" | ||
#include "../common/types.h" | ||
|
||
#endif // LEXER_h | ||
/** | ||
* @brief Tokenizes a string. | ||
* | ||
* @param src The string to tokenize. | ||
* @return const Token* The tokens. | ||
*/ | ||
Token *tokenize(string src); | ||
|
||
#endif // LEXER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
#include <stdio.h> | ||
|
||
#include "lexer/lexer.h" | ||
#include "common/types.h" | ||
|
||
int main(void) | ||
{ | ||
printf("Hello, world!\n"); | ||
return 0; | ||
string src = "let x = 5"; | ||
Token *tokens = tokenize(src); | ||
for (int i = 0; i < sizeof(tokens); i++) | ||
{ | ||
printf("Token: %s\n", tokens[i].value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let x = 45 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef UTILS_INTS_C | ||
#define UTILS_INTS_C | ||
|
||
#include <stdbool.h> | ||
|
||
/** | ||
* @brief Checks if a character is an integer. | ||
* | ||
* @param c The character to check. | ||
* @return true If the character is an integer. | ||
* @return false If the character is not an integer. | ||
*/ | ||
bool is_int(char c) | ||
{ | ||
return c >= '0' && c <= '9'; | ||
} | ||
|
||
#endif // UTILS_INTS_C |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef UTILS_INTS_H | ||
#define UTILS_INTS_H | ||
|
||
#include <stdbool.h> | ||
|
||
/** | ||
* @brief Checks if a character is an integer. | ||
* | ||
* @param c The character to check. | ||
* @return true If the character is an integer. | ||
* @return false If the character is not an integer. | ||
*/ | ||
bool is_int(char c); | ||
|
||
#endif // UTILS_INTS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef UTILS_STRINGS_H | ||
#define UTILS_STRINGS_H | ||
|
||
#include "../common/types.h" | ||
|
||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
/** | ||
* @brief Splits a string by a delimiter. | ||
* | ||
* @param src The string to split. | ||
* @param delim The delimiter to split by. | ||
* @return string* The split string. | ||
*/ | ||
string *split(string src, string delim) | ||
{ | ||
string *res = malloc(sizeof(string)); | ||
string token = strtok(src, delim); | ||
|
||
while (token != NULL) | ||
{ | ||
res[sizeof(res) + 1] = token; | ||
token = strtok(NULL, delim); | ||
} | ||
return res; | ||
} | ||
|
||
/** | ||
* @brief Checks if a character is a digit. | ||
* | ||
* @param c The character to check. | ||
* @return true If the character is a digit. | ||
* @return false If the character is not a digit. | ||
*/ | ||
bool is_alpha(char c) | ||
{ | ||
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); | ||
} | ||
|
||
#endif // UTILS_STRING_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef UTILS_STRINGS_H | ||
#define UTILS_STRINGS_H | ||
|
||
#include "../common/types.h" | ||
#include <stdbool.h> | ||
|
||
/** | ||
* @brief Splits a string by a delimiter. | ||
* | ||
* @param src The string to split. | ||
* @param delim The delimiter to split by. | ||
* @return string* The split string. | ||
*/ | ||
string *split(string src, string delim); | ||
|
||
/** | ||
* @brief Checks if a character is a digit. | ||
* | ||
* @param c The character to check. | ||
* @return true If the character is a digit. | ||
* @return false If the character is not a digit. | ||
*/ | ||
bool is_alpha(char c); | ||
|
||
#endif // UTILS_STRING_H |