Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
realTristan committed Jul 22, 2023
1 parent c2af613 commit 47a2b38
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 17 deletions.
19 changes: 19 additions & 0 deletions .vscode/c_cpp_properties.json
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
}
4 changes: 3 additions & 1 deletion .vscode/settings.json
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"
}
}
2 changes: 1 addition & 1 deletion Makefile
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 modified build/main
Binary file not shown.
6 changes: 6 additions & 0 deletions src/common/types.h
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
132 changes: 129 additions & 3 deletions src/lexer/lexer.c
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
16 changes: 13 additions & 3 deletions src/lexer/lexer.h
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
2 changes: 1 addition & 1 deletion src/lexer/token.h → src/lexer/token/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define TOKEN_H

#include "token_type.h"
#include "types.h"
#include "../../common/types.h"

/**
* @brief A token is a pair of a token type and a value.
Expand Down
1 change: 1 addition & 0 deletions src/lexer/token_type.h → src/lexer/token/token_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
typedef enum TokenType
{
TOKEN_TYPE_EOF,
TOKEN_TYPE_LET,
TOKEN_TYPE_NUMBER,
TOKEN_TYPE_PLUS,
TOKEN_TYPE_MINUS,
Expand Down
6 changes: 0 additions & 6 deletions src/lexer/types.h

This file was deleted.

11 changes: 9 additions & 2 deletions src/main.c
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);
}
}
1 change: 1 addition & 0 deletions src/main.simpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x = 45
18 changes: 18 additions & 0 deletions src/utils/ints.c
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
15 changes: 15 additions & 0 deletions src/utils/ints.h
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
42 changes: 42 additions & 0 deletions src/utils/strings.c
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
25 changes: 25 additions & 0 deletions src/utils/strings.h
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

0 comments on commit 47a2b38

Please sign in to comment.