Finished initial AST structures.
Call and ints (nums).
This commit is contained in:
parent
68fc644ea6
commit
891d8bf7ef
54
src/ast.c
Normal file
54
src/ast.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "include/ast.h"
|
||||||
|
#include "include/util.h"
|
||||||
|
|
||||||
|
static char* asttype_names[] = {
|
||||||
|
[AST_TYPE_CALL] = "CALL",
|
||||||
|
[AST_TYPE_NUM] = "NUMBER",
|
||||||
|
};
|
||||||
|
|
||||||
|
ASTTypeNum* ast_type_num_init(int val) {
|
||||||
|
talloc(ASTTypeNum, num);
|
||||||
|
|
||||||
|
num->val = val;
|
||||||
|
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_type_num_destroy(ASTTypeNum* num) {
|
||||||
|
free(num);
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTTypeCall* ast_type_call_init(char* to, size_t argc, AST** argv) {
|
||||||
|
talloc(ASTTypeCall, call);
|
||||||
|
|
||||||
|
call->to = to;
|
||||||
|
call->argc = argc;
|
||||||
|
call->argv = argv;
|
||||||
|
|
||||||
|
return call;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_type_call_destroy(ASTTypeCall* call) {
|
||||||
|
free(call->to);
|
||||||
|
for (size_t i = 0; i < call->argc; i++) free(call->argv[i]);
|
||||||
|
free(call);
|
||||||
|
}
|
||||||
|
|
||||||
|
AST* ast_init(ASTType type, void* data) {
|
||||||
|
AST* ast = malloc(sizeof(AST));
|
||||||
|
|
||||||
|
ast->type = type;
|
||||||
|
ast->data = data;
|
||||||
|
|
||||||
|
return ast;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_destroy(AST* ast) {
|
||||||
|
switch (ast->type) {
|
||||||
|
case AST_TYPE_NUM: ast_type_num_destroy(ast->data); break;
|
||||||
|
case AST_TYPE_CALL: ast_type_call_destroy(ast->data); break;
|
||||||
|
default: log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX);
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,12 @@
|
|||||||
#ifndef AST_H
|
#ifndef AST_H
|
||||||
#define AST_H
|
#define AST_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
AST_TYPE_NUM,
|
AST_TYPE_NUM,
|
||||||
AST_TYPE,CALL
|
AST_TYPE_CALL,
|
||||||
|
AST_TYPE_MAX = AST_TYPE_CALL
|
||||||
} ASTType;
|
} ASTType;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -15,9 +18,19 @@ typedef struct {
|
|||||||
int val;
|
int val;
|
||||||
} ASTTypeNum;
|
} ASTTypeNum;
|
||||||
|
|
||||||
|
ASTTypeNum* ast_type_num_init(int val);
|
||||||
|
void ast_type_num_destroy(ASTTypeNum* num);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* to;
|
char* to; // What the call's to.
|
||||||
char** args;
|
size_t argc; // Argument count.
|
||||||
|
AST** argv; // Argument vector.
|
||||||
} ASTTypeCall;
|
} ASTTypeCall;
|
||||||
|
|
||||||
|
ASTTypeCall* ast_type_call_init(char* to, size_t argc, AST** argv);
|
||||||
|
void ast_type_call_destroy(ASTTypeCall* call);
|
||||||
|
|
||||||
|
AST* ast_init(ASTType type, void* data);
|
||||||
|
void ast_destroy(AST* ast);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,13 +1,4 @@
|
|||||||
#ifndef PARSER_H
|
#ifndef PARSER_H
|
||||||
#define PARSER_H
|
#define PARSER_H
|
||||||
|
|
||||||
// Expression one of:
|
|
||||||
// - Operation
|
|
||||||
// - Number
|
|
||||||
|
|
||||||
// Operation contains:
|
|
||||||
// - Type
|
|
||||||
// - Expression 1
|
|
||||||
// - Expression 2
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -40,6 +40,10 @@
|
|||||||
// Print & indent a thing without any newline.
|
// Print & indent a thing without any newline.
|
||||||
#define INDENT_FIELD_NONL(FIELD) printf("%s " FIELD ": ", INDENT_spacing->buf);
|
#define INDENT_FIELD_NONL(FIELD) printf("%s " FIELD ": ", INDENT_spacing->buf);
|
||||||
|
|
||||||
|
// End an indent block.
|
||||||
#define INDENT_END dstr_destroy(INDENT_spacing);
|
#define INDENT_END dstr_destroy(INDENT_spacing);
|
||||||
|
|
||||||
|
// Allocate a pointer with a type.
|
||||||
|
#define talloc(T, X) T* X = malloc(sizeof(T));
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
15
src/lexer.c
15
src/lexer.c
@ -41,7 +41,10 @@ void lexer_lex(Lexer* lexer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void lexer_do_confused(Lexer* lexer) {
|
void lexer_do_confused(Lexer* lexer) {
|
||||||
log_dbgf("lexer @ %p entered confused mode @ char '%c' (%d)", lexer, *lexer->cchar, (int)*lexer->cchar);
|
log_dbgf("lexer @ %p entered confused mode @ char '%c' (%d)", lexer,
|
||||||
|
*lexer->cchar, (int)*lexer->cchar);
|
||||||
|
|
||||||
|
if (isspace(*lexer->cchar)) lexer_inc(lexer);
|
||||||
|
|
||||||
if (isdigit(*lexer->cchar)) {
|
if (isdigit(*lexer->cchar)) {
|
||||||
lexer->state = LEXER_STATE_NUM;
|
lexer->state = LEXER_STATE_NUM;
|
||||||
@ -53,7 +56,8 @@ void lexer_do_confused(Lexer* lexer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void lexer_do_number(Lexer* lexer) {
|
void lexer_do_number(Lexer* lexer) {
|
||||||
log_dbgf("lexer @ %p entered number mode @ char '%c' (%d)", lexer, *lexer->cchar, (int)*lexer->cchar);
|
log_dbgf("lexer @ %p entered number mode @ char '%c' (%d)", lexer,
|
||||||
|
*lexer->cchar, (int)*lexer->cchar);
|
||||||
|
|
||||||
// Length of the number string.
|
// Length of the number string.
|
||||||
size_t numln;
|
size_t numln;
|
||||||
@ -73,7 +77,8 @@ void lexer_do_number(Lexer* lexer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void lexer_do_call(Lexer* lexer) {
|
void lexer_do_call(Lexer* lexer) {
|
||||||
log_dbgf("lexer @ %p entered call mode @ char '%c' (%d)", lexer, *lexer->cchar, (int)*lexer->cchar);
|
log_dbgf("lexer @ %p entered call mode @ char '%c' (%d)", lexer,
|
||||||
|
*lexer->cchar, (int)*lexer->cchar);
|
||||||
|
|
||||||
// Size of the call string.
|
// Size of the call string.
|
||||||
size_t callln;
|
size_t callln;
|
||||||
@ -81,7 +86,9 @@ void lexer_do_call(Lexer* lexer) {
|
|||||||
// Where the call string starts.
|
// Where the call string starts.
|
||||||
char* start = lexer->cchar;
|
char* start = lexer->cchar;
|
||||||
|
|
||||||
for (callln = 0; *lexer->cchar && (!isdigit(*lexer->cchar)); callln++)
|
for (callln = 0;
|
||||||
|
*lexer->cchar && (!isdigit(*lexer->cchar) && !isspace(*lexer->cchar));
|
||||||
|
callln++)
|
||||||
lexer_inc(lexer);
|
lexer_inc(lexer);
|
||||||
|
|
||||||
char* call = malloc(callln + 1);
|
char* call = malloc(callln + 1);
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "include/dstr.h"
|
#include "include/dstr.h"
|
||||||
#include "include/token.h"
|
|
||||||
#include "include/util.h"
|
#include "include/util.h"
|
||||||
#include "include/lexer.h"
|
#include "include/lexer.h"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user