root/crowbar.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. PARSE_ERR
  2. CHARACTER_INVALID_ERR
  3. FUNCTION_MULTIPLE_DEFINE_ERR
  4. COMPILE_ERROR_COUNT_PLUS_1
  5. CompileError
  6. VARIABLE_NOT_FOUND_ERR
  7. FUNCTION_NOT_FOUND_ERR
  8. ARGUMENT_TOO_MANY_ERR
  9. ARGUMENT_TOO_FEW_ERR
  10. NOT_BOOLEAN_TYPE_ERR
  11. MINUS_OPERAND_TYPE_ERR
  12. STRING_OPERATOR_ERR
  13. BAD_OPERAND_TYPE_ERR
  14. LOGICAL_OP_DOUBLE_OPERAND_ERR
  15. LOGICAL_OP_INTEGER_OPERAND_ERR
  16. NOT_BOOLEAN_OPERATOR_ERR
  17. FOPEN_ARGUMENT_TYPE_ERR
  18. FCLOSE_ARGUMENT_TYPE_ERR
  19. FGETS_ARGUMENT_TYPE_ERR
  20. FPUTS_ARGUMENT_TYPE_ERR
  21. NOT_NULL_OPERATOR_ERR
  22. NOT_LVALUE_ERR
  23. INDEX_OPERAND_NOT_ARRAY_ERR
  24. INDEX_OPERAND_NOT_INT_ERR
  25. ARRAY_INDEX_OUT_OF_BOUNDS_ERR
  26. NO_SUCH_METHOD_ERR
  27. NEW_ARRAY_ARGUMENT_TYPE_ERR
  28. INC_DEC_OPERAND_TYPE_ERR
  29. RUNTIME_ERROR_COUNT_PLUS_1
  30. RuntimeError
  31. INT_MESSAGE_ARGUMENT
  32. DOUBLE_MESSAGE_ARGUMENT
  33. STRING_MESSAGE_ARGUMENT
  34. CHARACTER_MESSAGE_ARGUMENT
  35. POINTER_MESSAGE_ARGUMENT
  36. MESSAGE_ARGUMENT_END
  37. MessageArgumentType
  38. MessageFormat
  39. BOOLEAN_EXPRESSION
  40. INT_EXPRESSION
  41. DOUBLE_EXPRESSION
  42. STRING_EXPRESSION
  43. IDENTIFIER_EXPRESSION
  44. ASSIGN_EXPRESSION
  45. ADD_EXPRESSION
  46. SUB_EXPRESSION
  47. MUL_EXPRESSION
  48. DIV_EXPRESSION
  49. MOD_EXPRESSION
  50. LOGICAL_AND_EXPRESSION
  51. LOGICAL_OR_EXPRESSION
  52. EQ_EXPRESSION
  53. NE_EXPRESSION
  54. GT_EXPRESSION
  55. GE_EXPRESSION
  56. LT_EXPRESSION
  57. LE_EXPRESSION
  58. MINUS_EXPRESSION
  59. FUNCTION_CALL_EXPRESSION
  60. METHOD_CALL_EXPRESSION
  61. NULL_EXPRESSION
  62. ARRAY_EXPRESSION
  63. INDEX_EXPRESSION
  64. INCREMENT_EXPRESSION
  65. DECREMENT_EXPRESSION
  66. EXPRESSION_TYPE_COUNT_PLUS_1
  67. ExpressionType
  68. AssignExpression
  69. BinaryExpression
  70. FunctionCallExpression
  71. ExpressionList
  72. IndexExpression
  73. MethodCallExpression
  74. IncrementOrDecrement
  75. StatementList
  76. Block
  77. Elsif
  78. IfStatement
  79. WhileStatement
  80. ForStatement
  81. ReturnStatement
  82. EXPRESSION_STATEMENT
  83. IF_STATEMENT
  84. WHILE_STATEMENT
  85. FOR_STATEMENT
  86. RETURN_STATEMENT
  87. BREAK_STATEMENT
  88. CONTINUE_STATEMENT
  89. STATEMENT_TYPE_COUNT_PLUS_1
  90. StatementType
  91. ParameterList
  92. CROWBAR_FUNCTION_DEFINITION
  93. NATIVE_FUNCTION_DEFINITION
  94. FUNCTION_DEFINITION_TYPE_COUNT_PLUS_1
  95. FunctionDefinitionType
  96. FunctionDefinition
  97. Variable
  98. NORMAL_STATEMENT_RESULT
  99. RETURN_STATEMENT_RESULT
  100. BREAK_STATEMENT_RESULT
  101. CONTINUE_STATEMENT_RESULT
  102. STATEMENT_RESULT_TYPE_COUNT_PLUS_1
  103. StatementResultType
  104. StatementResult
  105. LocalEnvironment
  106. Stack
  107. Heap
  108. ARRAY_OBJECT
  109. STRING_OBJECT
  110. OBJECT_TYPE_COUNT_PLUS_1
  111. ObjectType

#ifndef PRIVATE_CROWBAR_H_INCLUDED
#define PRIVATE_CROWBAR_H_INCLUDED
#include <stdio.h>
#include <setjmp.h>
#include "MEM.h"
#include "CRB.h"
#include "CRB_dev.h"

#define smaller(a, b) ((a) < (b) ? (a) : (b))
#define larger(a, b) ((a) > (b) ? (a) : (b))

#define MESSAGE_ARGUMENT_MAX    (256)
#define LINE_BUF_SIZE           (1024)
#define STACK_ALLOC_SIZE        (256)
#define ARRAY_ALLOC_SIZE        (256)
#define HEAP_THRESHOLD_SIZE     (1024 * 256)

typedef enum {
    PARSE_ERR = 1,
    CHARACTER_INVALID_ERR,
    FUNCTION_MULTIPLE_DEFINE_ERR,
    COMPILE_ERROR_COUNT_PLUS_1
} CompileError;

typedef enum {
    VARIABLE_NOT_FOUND_ERR = 1,
    FUNCTION_NOT_FOUND_ERR,
    ARGUMENT_TOO_MANY_ERR,
    ARGUMENT_TOO_FEW_ERR,
    NOT_BOOLEAN_TYPE_ERR,
    MINUS_OPERAND_TYPE_ERR,
    STRING_OPERATOR_ERR,
    BAD_OPERAND_TYPE_ERR,
    LOGICAL_OP_DOUBLE_OPERAND_ERR,
    LOGICAL_OP_INTEGER_OPERAND_ERR,
    NOT_BOOLEAN_OPERATOR_ERR,
    FOPEN_ARGUMENT_TYPE_ERR,
    FCLOSE_ARGUMENT_TYPE_ERR,
    FGETS_ARGUMENT_TYPE_ERR,
    FPUTS_ARGUMENT_TYPE_ERR,
    NOT_NULL_OPERATOR_ERR,
    NOT_LVALUE_ERR,
    INDEX_OPERAND_NOT_ARRAY_ERR,
    INDEX_OPERAND_NOT_INT_ERR,
    ARRAY_INDEX_OUT_OF_BOUNDS_ERR,
    NO_SUCH_METHOD_ERR,
    NEW_ARRAY_ARGUMENT_TYPE_ERR,
    INC_DEC_OPERAND_TYPE_ERR,
    RUNTIME_ERROR_COUNT_PLUS_1
} RuntimeError;

typedef enum {
    INT_MESSAGE_ARGUMENT = 1,
    DOUBLE_MESSAGE_ARGUMENT,
    STRING_MESSAGE_ARGUMENT,
    CHARACTER_MESSAGE_ARGUMENT,
    POINTER_MESSAGE_ARGUMENT,
    MESSAGE_ARGUMENT_END
} MessageArgumentType;

typedef struct {
    char *format;
} MessageFormat;

typedef struct Expression_tag Expression;

typedef enum {
    BOOLEAN_EXPRESSION = 1,
    INT_EXPRESSION,
    DOUBLE_EXPRESSION,
    STRING_EXPRESSION,
    IDENTIFIER_EXPRESSION,
    ASSIGN_EXPRESSION,
    ADD_EXPRESSION,
    SUB_EXPRESSION,
    MUL_EXPRESSION,
    DIV_EXPRESSION,
    MOD_EXPRESSION,
    LOGICAL_AND_EXPRESSION,
    LOGICAL_OR_EXPRESSION,
    EQ_EXPRESSION,
    NE_EXPRESSION,
    GT_EXPRESSION,
    GE_EXPRESSION,
    LT_EXPRESSION,
    LE_EXPRESSION,
    MINUS_EXPRESSION,
    FUNCTION_CALL_EXPRESSION,
    METHOD_CALL_EXPRESSION,
    NULL_EXPRESSION,
    ARRAY_EXPRESSION,
    INDEX_EXPRESSION,
    INCREMENT_EXPRESSION,
    DECREMENT_EXPRESSION,
    EXPRESSION_TYPE_COUNT_PLUS_1
} ExpressionType;

#define dkc_is_math_operator(operator) \
  ((operator) == ADD_EXPRESSION || (operator) == SUB_EXPRESSION\
   || (operator) == MUL_EXPRESSION || (operator) == DIV_EXPRESSION\
   || (operator) == MOD_EXPRESSION)

#define dkc_is_compare_operator(operator) \
  ((operator) == EQ_EXPRESSION || (operator) == NE_EXPRESSION\
   || (operator) == GT_EXPRESSION || (operator) == GE_EXPRESSION\
   || (operator) == LT_EXPRESSION || (operator) == LE_EXPRESSION)

#define dkc_is_logical_operator(operator) \
  ((operator) == LOGICAL_AND_EXPRESSION || (operator) == LOGICAL_OR_EXPRESSION)

typedef struct ArgumentList_tag {
    Expression *expression;
    struct ArgumentList_tag *next;
} ArgumentList;

typedef struct {
    Expression  *left;
    Expression  *operand;
} AssignExpression;

typedef struct {
    Expression  *left;
    Expression  *right;
} BinaryExpression;

typedef struct {
    char                *identifier;
    ArgumentList        *argument;
} FunctionCallExpression;

typedef struct ExpressionList_tag {
    Expression          *expression;
    struct ExpressionList_tag   *next;
} ExpressionList;

typedef struct {
    Expression  *array;
    Expression  *index;
} IndexExpression;

typedef struct {
    Expression          *expression;
    char                *identifier;
    ArgumentList        *argument;
} MethodCallExpression;

typedef struct {
    Expression  *operand;
} IncrementOrDecrement;

struct Expression_tag {
    ExpressionType type;
    int line_number;
    union {
        CRB_Boolean             boolean_value;
        int                     int_value;
        double                  double_value;
        char                    *string_value;
        char                    *identifier;
        AssignExpression        assign_expression;
        BinaryExpression        binary_expression;
        Expression              *minus_expression;
        FunctionCallExpression  function_call_expression;
        MethodCallExpression    method_call_expression;
        ExpressionList          *array_literal;
        IndexExpression         index_expression;
        IncrementOrDecrement    inc_dec;
    } u;
};

typedef struct Statement_tag Statement;

typedef struct StatementList_tag {
    Statement   *statement;
    struct StatementList_tag    *next;
} StatementList;

typedef struct {
    StatementList       *statement_list;
} Block;

typedef struct Elsif_tag {
    Expression  *condition;
    Block       *block;
    struct Elsif_tag    *next;
} Elsif;

typedef struct {
    Expression  *condition;
    Block       *then_block;
    Elsif       *elsif_list;
    Block       *else_block;
} IfStatement;

typedef struct {
    Expression  *condition;
    Block       *block;
} WhileStatement;

typedef struct {
    Expression  *init;
    Expression  *condition;
    Expression  *post;
    Block       *block;
} ForStatement;

typedef struct {
    Expression *return_value;
} ReturnStatement;

typedef enum {
    EXPRESSION_STATEMENT = 1,
    IF_STATEMENT,
    WHILE_STATEMENT,
    FOR_STATEMENT,
    RETURN_STATEMENT,
    BREAK_STATEMENT,
    CONTINUE_STATEMENT,
    STATEMENT_TYPE_COUNT_PLUS_1
} StatementType;

struct Statement_tag {
    StatementType       type;
    int                 line_number;
    union {
        Expression      *expression_s;
        IfStatement     if_s;
        WhileStatement  while_s;
        ForStatement    for_s;
        ReturnStatement return_s;
    } u;
};

typedef struct ParameterList_tag {
    char        *name;
    struct ParameterList_tag *next;
} ParameterList;

typedef enum {
    CROWBAR_FUNCTION_DEFINITION = 1,
    NATIVE_FUNCTION_DEFINITION,
    FUNCTION_DEFINITION_TYPE_COUNT_PLUS_1
} FunctionDefinitionType;

typedef struct FunctionDefinition_tag {
    char                *name;
    FunctionDefinitionType      type;
    union {
        struct {
            ParameterList       *parameter;
            Block               *block;
        } crowbar_f;
        struct {
            CRB_NativeFunctionProc      *proc;
        } native_f;
    } u;
    struct FunctionDefinition_tag       *next;
} FunctionDefinition;

typedef struct Variable_tag {
    char        *name;
    CRB_Value   value;
    struct Variable_tag *next;
} Variable;

typedef enum {
    NORMAL_STATEMENT_RESULT = 1,
    RETURN_STATEMENT_RESULT,
    BREAK_STATEMENT_RESULT,
    CONTINUE_STATEMENT_RESULT,
    STATEMENT_RESULT_TYPE_COUNT_PLUS_1
} StatementResultType;

typedef struct {
    StatementResultType type;
    union {
        CRB_Value       return_value;
    } u;
} StatementResult;

typedef struct LocalEnvironment_tag {
    Variable            *variable;
    struct LocalEnvironment_tag *next;
} LocalEnvironment;

typedef struct {
    int         stack_alloc_size;
    int         stack_pointer;
    CRB_Value   *stack;
} Stack;

typedef struct {
    int         current_heap_size;
    int         current_threshold;
    CRB_Object  *header;
} Heap;

struct CRB_Interpreter_tag {
    MEM_Storage         interpreter_storage;
    MEM_Storage         execute_storage;
    Variable            *variable;
    FunctionDefinition  *function_list;
    StatementList       *statement_list;
    int                 current_line_number;
    Stack               stack;
    Heap                heap;
    LocalEnvironment    *top_environment;
};

struct CRB_Array_tag {
    int         size;
    int         alloc_size;
    CRB_Value   *array;
};

struct CRB_String_tag {
    CRB_Boolean is_literal;
    char        *string;
};

typedef enum {
    ARRAY_OBJECT = 1,
    STRING_OBJECT,
    OBJECT_TYPE_COUNT_PLUS_1
} ObjectType;

#define dkc_is_object_value(type) \
  ((type) == CRB_STRING_VALUE || (type == CRB_ARRAY_VALUE))

struct CRB_Object_tag {
    ObjectType  type;
    unsigned int        marked:1;
    union {
        CRB_Array       array;
        CRB_String      string;
    } u;
    struct CRB_Object_tag *prev;
    struct CRB_Object_tag *next;
};


/* create.c */
void crb_function_define(char *identifier, ParameterList *parameter_list,
                         Block *block);
ParameterList *crb_create_parameter(char *identifier);
ParameterList *crb_chain_parameter(ParameterList *list,
                                   char *identifier);
ArgumentList *crb_create_argument_list(Expression *expression);
ArgumentList *crb_chain_argument_list(ArgumentList *list, Expression *expr);
ExpressionList *crb_create_expression_list(Expression *expression);
ExpressionList *crb_chain_expression_list(ExpressionList *list,
                                        Expression *expr);
StatementList *crb_create_statement_list(Statement *statement);
StatementList *crb_chain_statement_list(StatementList *list,
                                        Statement *statement);
Expression *crb_alloc_expression(ExpressionType type);
Expression *crb_create_assign_expression(Expression *left,
                                         Expression *operand);
Expression *crb_create_binary_expression(ExpressionType operator,
                                         Expression *left,
                                         Expression *right);
Expression *crb_create_minus_expression(Expression *operand);
Expression *crb_create_index_expression(Expression *array, Expression *index);
Expression *crb_create_incdec_expression(Expression *operand,
                                         ExpressionType inc_or_dec);
Expression *crb_create_identifier_expression(char *identifier);
Expression *crb_create_function_call_expression(char *func_name,
                                                ArgumentList *argument);
Expression *crb_create_method_call_expression(Expression *expression,
                                              char *method_name,
                                              ArgumentList *argument);
Expression *crb_create_boolean_expression(CRB_Boolean value);
Expression *crb_create_null_expression(void);
Expression *crb_create_array_expression(ExpressionList *list);

Statement *crb_create_if_statement(Expression *condition,
                                    Block *then_block, Elsif *elsif_list,
                                    Block *else_block);
Elsif *crb_chain_elsif_list(Elsif *list, Elsif *add);
Elsif *crb_create_elsif(Expression *expr, Block *block);
Statement *crb_create_while_statement(Expression *condition, Block *block);
Statement *crb_create_for_statement(Expression *init, Expression *cond,
                                    Expression *post, Block *block);
Block *crb_create_block(StatementList *statement_list);
Statement *crb_create_expression_statement(Expression *expression);
Statement *crb_create_return_statement(Expression *expression);
Statement *crb_create_break_statement(void);
Statement *crb_create_continue_statement(void);

/* string.c */
char *crb_create_identifier(char *str);
void crb_open_string_literal(void);
void crb_add_string_literal(int letter);
void crb_reset_string_literal_buffer(void);
char *crb_close_string_literal(void);

/* execute.c */
StatementResult
crb_execute_statement_list(CRB_Interpreter *inter,
                           LocalEnvironment *env, StatementList *list);

/* eval.c */
CRB_Value crb_eval_binary_expression(CRB_Interpreter *inter,
                                 LocalEnvironment *env,
                                 ExpressionType operator,
                                 Expression *left, Expression *right);
CRB_Value crb_eval_minus_expression(CRB_Interpreter *inter,
                                LocalEnvironment *env, Expression *operand);
CRB_Value crb_eval_expression(CRB_Interpreter *inter,
                          LocalEnvironment *env, Expression *expr);

/* heap.c */
CRB_Object *crb_literal_to_crb_string(CRB_Interpreter *inter, char *str);
CRB_Object *crb_create_crowbar_string(CRB_Interpreter *inter, char *str);
CRB_Object *crb_create_array(CRB_Interpreter *inter, int size);
void crb_array_add(CRB_Interpreter *inter, CRB_Object *obj, CRB_Value v);
void crb_garbage_collect(CRB_Interpreter *inter);


/* util.c */
CRB_Interpreter *crb_get_current_interpreter(void);
void crb_set_current_interpreter(CRB_Interpreter *inter);
void *crb_malloc(size_t size);
void *crb_execute_malloc(CRB_Interpreter *inter, size_t size);
CRB_Value *crb_search_local_variable(LocalEnvironment *env,
                                 char *identifier);
CRB_Value *
crb_search_global_variable(CRB_Interpreter *inter, char *identifier);
CRB_Value *crb_search_local_variable(LocalEnvironment *env,
                                 char *identifier);
Variable *crb_add_local_variable(LocalEnvironment *env, char *identifier);
Variable *crb_add_global_variable(CRB_Interpreter *inter, char *identifier);
CRB_NativeFunctionProc *
crb_search_native_function(CRB_Interpreter *inter, char *name);
FunctionDefinition *crb_search_function(char *name);
char *crb_get_operator_string(ExpressionType type);

/* error.c */
void crb_compile_error(CompileError id, ...);
void crb_runtime_error(int line_number, RuntimeError id, ...);

/* native.c */
CRB_Value crb_nv_print_proc(CRB_Interpreter *interpreter,
                            int arg_count, CRB_Value *args);
CRB_Value crb_nv_fopen_proc(CRB_Interpreter *interpreter,
                            int arg_count, CRB_Value *args);
CRB_Value crb_nv_fclose_proc(CRB_Interpreter *interpreter,
                             int arg_count, CRB_Value *args);
CRB_Value crb_nv_fgets_proc(CRB_Interpreter *interpreter,
                            int arg_count, CRB_Value *args);
CRB_Value crb_nv_fputs_proc(CRB_Interpreter *interpreter,
                            int arg_count, CRB_Value *args);
CRB_Value crb_nv_new_array_proc(CRB_Interpreter *interpreter,
                                int arg_count, CRB_Value *args);


#endif /* PRIVATE_CROWBAR_H_INCLUDED */

/* [<][>][^][v][top][bottom][index][help] */