small renaming

This commit is contained in:
me 2026-01-08 00:24:21 +02:00
parent 16e8ec36e1
commit b5426dcc3f
7 changed files with 34 additions and 33 deletions

View file

@ -6,7 +6,7 @@ typedef struct {
unsigned length;
} Array;
typedef struct Expr {
typedef struct {
enum {
LITERAL,
VARIABLE,

View file

@ -1,18 +1,18 @@
#include <stdlib.h>
#include <stdio.h>
#include "lexer.h"
#include "parser.h"
#include "execute.h"
#include "ast.h"
#include "lex.h"
#include "parse.h"
#include "execute.h"
void run(char* code, unsigned length) {
TokenArray tokens = scan(code, length);
TokenArray tokens = lex(code, length);
puts("Tokens:\n");
print_TokenArray(tokens);
puts("\n");
StmtArray program = parse_program(tokens);
StmtArray program = parse(tokens);
puts("\nProgram:\n");
print_ast(program);

View file

@ -1,8 +1,11 @@
#include <stdlib.h>
#include <stdio.h>
#include "lexer.h"
#include "lex.h"
TokenArray scan(char* txt, unsigned length) {
int is_identifier_char(char);
int is_numeric(char);
TokenArray lex(char* txt, unsigned length) {
unsigned txt_index = 0;
Token* tokens = (Token*)malloc(length * sizeof(Token));
unsigned tokens_index = 0;
@ -53,10 +56,10 @@ TokenArray scan(char* txt, unsigned length) {
break;
}
default: {
if (is_alpha(txt[txt_index])) {
if (is_identifier_char(txt[txt_index])) {
char* word = malloc(128);
unsigned word_index = 0;
while (txt_index < length && txt[txt_index] != '\0' && is_alpha(txt[txt_index]) && word_index < 128) {
while (txt_index < length && txt[txt_index] != '\0' && is_identifier_char(txt[txt_index]) && word_index < 128) {
word[word_index] = txt[txt_index];
++word_index;
++txt_index;
@ -84,7 +87,7 @@ TokenArray scan(char* txt, unsigned length) {
return (TokenArray){ .tokens = tokens, .length = tokens_index };
}
int is_alpha(char c) {
int is_identifier_char(char c) {
return c == '_' || ('a' <= c && c <= 'z');
}
@ -99,28 +102,28 @@ void print_TokenArray(TokenArray tokens) {
++token_index;
switch (token.tag) {
case IDENTIFIER:
printf("'%s' ", token.data.identifier);
printf("IDENTIFIER '%s'\n", token.data.identifier);
break;
case INTEGER:
printf("%d ", token.data.integer);
printf("NUMBER %d\n", token.data.integer);
break;
case OPENPAREN:
printf("( ");
printf("OPENPAREN\n");
break;
case CLOSEPAREN:
printf(") ");
printf("CLOSEPAREN\n");
break;
case OPENCURLY:
printf("{ ");
printf("OPENCURLY\n");
break;
case CLOSECURLY:
printf("} ");
printf("CLOSECURLY\n");
break;
case EQUALS:
printf("= ");
printf("EQUALS\n");
break;
case COMMA:
printf(", ");
printf("COMMA\n");
break;
}
}

View file

@ -25,9 +25,7 @@ typedef struct {
unsigned length;
} TokenArray;
TokenArray scan(char*, unsigned);
int is_alpha(char);
int is_numeric(char);
TokenArray lex(char*, unsigned);
void print_TokenArray(TokenArray);

View file

@ -1,4 +1,4 @@
#include "parser.h"
#include "parse.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@ -154,7 +154,7 @@ StmtArray parse_block(TokenArray tokens, unsigned* tokens_index) {
}
StmtArray parse_program(TokenArray tokens) {
StmtArray parse(TokenArray tokens) {
unsigned tokens_index = 0;
unsigned stmt_index = 0;
Stmt* stmts = (Stmt*)malloc(tokens.length * sizeof(Stmt));

View file

@ -0,0 +1,9 @@
#ifndef PARSER_H
#define PARSER_H
#include "ast.h"
#include "lex.h"
StmtArray parse(TokenArray tokens);
#endif

View file

@ -1,9 +0,0 @@
#ifndef PARSER_H
#define PARSER_H
#include "lexer.h"
#include "ast.h"
StmtArray parse_program(TokenArray tokens);
#endif