codebased/lommereimar/calculator.h
Reimar f4b2d113f7 lommereimar: add lommereimar
This commit adds the required files for the LommeReimar project. It is very good and nice. also incomplete.
2023-04-19 20:13:25 +00:00

116 lines
1.8 KiB
C

#ifndef CALCULATOR_H
#define CALCULATOR_H
typedef struct {
FILE* file;
bool done;
} FileIter;
FileIter fileiter(const char* filename);
char fileiter_next(FileIter* file);
typedef struct {
int line;
int col;
} Position;
typedef enum {
TokenTypeEof,
TokenTypeInvalidChar,
TokenTypeInt,
TokenTypePlus,
TokenTypeMinus,
TokenTypeAsterisk,
TokenTypeSlash,
TokenTypeHat,
TokenTypeLParen,
TokenTypeRParen,
} TokenType;
typedef struct {
// optional
char* value;
TokenType type;
Position pos;
} Token;
typedef struct {
FileIter input_file;
char current;
Position pos;
} Lexer;
Lexer lexer(FileIter input_file);
void lexer_step(Lexer* lexer);
Token lexer_single_token(Lexer* lexer, TokenType token_type);
Token lexer_next(Lexer* lexer);
typedef enum {
ExprTypeEof,
ExprTypeError,
ExprTypeInt,
ExprTypeFloat,
ExprTypeUnary,
ExprTypeBinary,
} ExprType;
typedef struct Expr Expr;
typedef enum {
ExprBinaryTypeAdd,
ExprBinaryTypeSubtract,
ExprBinaryTypeMultiply,
ExprBinaryTypeDivide,
ExprBinaryTypeExponent,
} ExprBinaryType;
typedef struct {
ExprBinaryType type;
Expr* left;
Expr* right;
} ExprBinary;
typedef enum {
ExprUnaryTypeNegate,
} ExprUnaryType;
typedef struct {
ExprUnaryType type;
Expr* subject;
} ExprUnary;
struct Expr {
ExprType type;
Position pos;
union {
char* error_value;
int int_value;
float float_value;
ExprUnary unary;
ExprBinary binary;
};
};
Expr* allocate_expr(Expr data);
typedef struct {
Lexer lexer;
Token current;
} Parser;
Parser parser(Lexer lexer);
void parser_step(Parser* parser);
Expr* parser_parse_expr(Parser* parser);
Expr* parser_parse_term(Parser* parser);
Expr* parser_parse_factor(Parser* parser);
#endif