Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add structures we'll need for the first parser iteration #44

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/parser/cst.h
Original file line number Diff line number Diff line change
@@ -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;
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 {
char name[256];
} FunctionCall;

// An expression for now is an integer or a function call.
typedef struct {
union {
FunctionCall fc;
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;
Loading