From ef78539671f7566417acb0501a5d6ef1f284f3a3 Mon Sep 17 00:00:00 2001 From: adamhutchings Date: Wed, 26 Jun 2024 13:19:32 -0400 Subject: [PATCH 1/2] Add structures we'll need for the first parser iteration --- src/parser/cst.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/parser/cst.h diff --git a/src/parser/cst.h b/src/parser/cst.h new file mode 100644 index 0000000..59fa469 --- /dev/null +++ b/src/parser/cst.h @@ -0,0 +1,62 @@ +/** + * The structures for a concrete syntax tree. + * For now, the subset of C we are parsing is quite simple: + * - Parameterless functions. + * - Return statements, which accept integers or function calls. + */ + +#pragma once + +#include "list.h" + +// A list of all node types. +typedef enum { + NT_STMT, + NT_EXPR, + NT_BLOCK_STMT, + NT_RETURN_STMT, + NT_FUNCDECL, + NT_FUNCCALL, + NT_LITERAL, +} NodeType; + +// A block statement is just a list of statements. +typedef struct { + List* stmts; // A list of Statement structs. +} BlockStatement; + +typedef struct { + // TODO -- add parameters whe we get there + BlockStatement body; + const char name[256]; // The actual name of the function. +} FunctionDeclaration; + +// An entire program is just a list of top level declarations. +// For now, such declarations are only functions. +typedef struct { + union { + FunctionDeclaration fd; + // VariableDeclaration vd; when we get there + } u; + NodeType type; +} TopLevelDeclaration; + +// Right now, a function call doesn't have any parameters so it's just the name +// of the function being called. +typedef struct { + const char name[256]; +} FunctionCall; + +// An expression for now is an integer or a function call. +typedef struct { + union { + FunctionCall fc; + const char literal[256]; + } u; + NodeType type; +} Expression; + +// Finally, an entire source file is a list of top-level declarations. +typedef struct { + List* decls; // list of TopLevelDeclaration +} ConcreteFileTree; From e669ebf8f2f0305d8a8ed04df118674b3af5bc9f Mon Sep 17 00:00:00 2001 From: adamhutchings Date: Wed, 26 Jun 2024 13:21:48 -0400 Subject: [PATCH 2/2] Remove consts (because we need to set these names somehow) --- src/parser/cst.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/parser/cst.h b/src/parser/cst.h index 59fa469..3184cf5 100644 --- a/src/parser/cst.h +++ b/src/parser/cst.h @@ -28,7 +28,7 @@ typedef struct { typedef struct { // TODO -- add parameters whe we get there BlockStatement body; - const char name[256]; // The actual name of the function. + char name[256]; // The actual name of the function. } FunctionDeclaration; // An entire program is just a list of top level declarations. @@ -44,14 +44,14 @@ typedef struct { // Right now, a function call doesn't have any parameters so it's just the name // of the function being called. typedef struct { - const char name[256]; + char name[256]; } FunctionCall; // An expression for now is an integer or a function call. typedef struct { union { FunctionCall fc; - const char literal[256]; + char literal[256]; } u; NodeType type; } Expression;