diff options
| author | Nakidai <plaza521@inbox.ru> | 2024-08-24 14:29:55 +0300 |
|---|---|---|
| committer | Nakidai <plaza521@inbox.ru> | 2024-08-24 14:29:55 +0300 |
| commit | c74aea420c662039072f606b2d5ef1c73426e481 (patch) | |
| tree | 4790fa17644df9e11380d6c02b8928c923c20aba /include | |
| parent | 2b0e05cbc1e4d9beccd3a5867c8730880f6ecc10 (diff) | |
| download | 3cl-c74aea420c662039072f606b2d5ef1c73426e481.tar.gz 3cl-c74aea420c662039072f606b2d5ef1c73426e481.zip | |
Add more code
Add some funcitons to work with variables, add more instructions and add ability to stop the code from `ccl_instruction`
Diffstat (limited to 'include')
| -rw-r--r-- | include/3cl.h | 2 | ||||
| -rw-r--r-- | include/instruction.h | 13 | ||||
| -rw-r--r-- | include/instructions.h | 21 | ||||
| -rw-r--r-- | include/main.h | 2 | ||||
| -rw-r--r-- | include/readchar.h | 44 | ||||
| -rw-r--r-- | include/readfile.h | 5 | ||||
| -rw-r--r-- | include/stack.h | 12 | ||||
| -rw-r--r-- | include/utils.h | 8 | ||||
| -rw-r--r-- | include/variable.h | 24 |
9 files changed, 124 insertions, 7 deletions
diff --git a/include/3cl.h b/include/3cl.h index a6b9065..4466490 100644 --- a/include/3cl.h +++ b/include/3cl.h @@ -1,6 +1,7 @@ #ifndef __3CL_H__ #define __3CL_H__ +#include <stdbool.h> #include <stddef.h> #include <stdint.h> @@ -85,6 +86,7 @@ struct CCL struct CCLStack stack; struct CCLFrame rootframe; const char *code; + bool stopped; int (*in)(); void (*out)(int); }; diff --git a/include/instruction.h b/include/instruction.h index 449c760..63f28b2 100644 --- a/include/instruction.h +++ b/include/instruction.h @@ -3,14 +3,15 @@ #include "3cl.h" - +/**< Type for every instruction in 3cl */ typedef struct CCLFrame *(*CCLInstruction)(struct CCL *ccl, struct CCLFrame *frame); -struct CCLFrame *ccl_instruction_nop(struct CCL *ccl, struct CCLFrame *frame); -struct CCLFrame *ccl_instruction_pushzero(struct CCL *ccl, struct CCLFrame *frame); -struct CCLFrame *ccl_instruction_increment(struct CCL *ccl, struct CCLFrame *frame); -struct CCLFrame *ccl_instruction_decrement(struct CCL *ccl, struct CCLFrame *frame); - +/** + * Execute next instruction + * @param ccl CCL instance + * @param frame Current frame + * @return Updated frame + */ struct CCLFrame *ccl_instruction(struct CCL *ccl, struct CCLFrame *frame); #endif /* __CCL_INSTRUCTION_H__ */ diff --git a/include/instructions.h b/include/instructions.h new file mode 100644 index 0000000..adada87 --- /dev/null +++ b/include/instructions.h @@ -0,0 +1,21 @@ +#ifndef __CCL_INSTRUCTIONS_H__ +#define __CCL_INSTRUCTIONS_H__ + +#include "3cl.h" + + +#define INST(NAME) \ + struct CCLFrame *ccl_instruction_##NAME(struct CCL *ccl, struct CCLFrame *frame) + +INST(nop); +INST(pushzero); +INST(increment); +INST(decrement); +INST(add); +INST(subtract); +INST(reverse); + +#undef INST +#define INST(NAME) ccl_instruction_##NAME + +#endif /* __CCL_INSTRUCTIONS_H__ */ diff --git a/include/main.h b/include/main.h index ad3bdc6..64abb24 100644 --- a/include/main.h +++ b/include/main.h @@ -1,6 +1,6 @@ #ifndef __MAIN_H__ #define __MAIN_H__ -extern const char *program_name; +extern const char *program_name; /**< argv[0] */ #endif /* __MAIN_H__ */ diff --git a/include/readchar.h b/include/readchar.h new file mode 100644 index 0000000..e881dcb --- /dev/null +++ b/include/readchar.h @@ -0,0 +1,44 @@ +#ifndef __CCL_READCHAR_H__ +#define __CCL_READCHAR_H__ + +#include "3cl.h" + + +/** + * Flags to tell ccl_readchar how to read next character + * + * Bits + * 4 Die if char is not whitelisted + * 3 Brackets + * 2 Instruction symbols + * 1 English alphabet + * 0 Underscore + * + * @see ccl_readchar + */ +enum CCLRCFlags +{ + CCL_RC_DIE = 0b10000, /**< Die if char is not whitelisted */ + CCL_RC_BRACK = 0b01000, /**< Brackes */ + CCL_RC_IS = 0b00100, /**< Instruction symbols */ + CCL_RC_ALP = 0b00010, /**< English alphabet */ + CCL_RC_US = 0b00001, /**< Underscore */ + CCL_RC_VAR = 0b11010, /**< (Used by 3CL) Variable names */ + CCL_RC_CCL_VARUS = 0b11011, /**< (Used by 3CL) Variable names including underscore */ + CCL_RC_CCL_BRACK = 0b01000, /**< (Used by 3CL) Brackets */ + CCL_RC_CCL_INSTR = 0b11111, /**< (Used by 3CL) Instruction */ +}; + +/** + * Function to read next character + * Skips whitespace and comments, returns '\0' if reaches EOF + * ccl_readchar will die on invalid symbol (e.g. cyrillic one) even if CCL_RC_DIE is set + * @see CCLRCFlags + * @param ccl CCL instance + * @param frame Current frame + * @param flags CCLRCFlags + * @return Next character + */ +char ccl_readchar(struct CCL *ccl, struct CCLFrame *frame, enum CCLRCFlags flags); + +#endif /* __CCL_READCHAR_H__ */ diff --git a/include/readfile.h b/include/readfile.h index 5358fc7..dcb47eb 100644 --- a/include/readfile.h +++ b/include/readfile.h @@ -1,6 +1,11 @@ #ifndef __READFILE_H__ #define __READFILE_H__ +/** + * Read file by filename, die on error + * @param name Path to file + * @return Contents of file with additional '\0' at the end + */ char *readfile(const char *name); #endif /* __READFILE_H__ */ diff --git a/include/stack.h b/include/stack.h index 27cc2f0..ac17ed3 100644 --- a/include/stack.h +++ b/include/stack.h @@ -3,7 +3,19 @@ #include "3cl.h" + +/** + * Push num to the stack + * @param stack Stack where to push + * @param num Number to push + */ void ccl_stack_push(struct CCLStack *stack, CCLNum num); + +/** + * Pop number from the stack + * @param stack Stack from where to pop + * @return Popped number + */ CCLNum ccl_stack_pop(struct CCLStack *stack); #endif /* __CCL_STACK_H__ */ diff --git a/include/utils.h b/include/utils.h index 40bb642..f415abc 100644 --- a/include/utils.h +++ b/include/utils.h @@ -3,6 +3,14 @@ #include <stdnoreturn.h> + +/** + * Show message: + * program_name: <formattedstring>\n + * and exit with code + * @param code Code to exit + * @param fmt Format string + */ noreturn void die(int code, char *fmt, ...); #endif /* __UTILS_H__ */ diff --git a/include/variable.h b/include/variable.h new file mode 100644 index 0000000..4480855 --- /dev/null +++ b/include/variable.h @@ -0,0 +1,24 @@ +#ifndef __CCL_VARIABLE_H__ +#define __CCL_VARIABLE_H__ + +#include "3cl.h" + + +/** + * Tries to find variable in list, NULL if not found. + * @see ccl_variable_getany + * @param vars Variable list + * @param name Variable name + */ +struct CCLVariable *ccl_variable_get(struct CCLVariable *vars, char name); + +/** + * Tries to find variable in current frame, then in root, NULL if not found. + * @see ccl_variable_get + * @param ccl CCL instance + * @param frame Current frame + * @param name Variable name + */ +struct CCLVariable *ccl_variable_getany(struct CCL *ccl, struct CCLFrame *frame, char name); + +#endif /* __CCL_VARIABLE_H__ */ |