This is a port of the mruby/c tutorial Chapter 03 to the mbed environment.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers value.h Source File

value.h

Go to the documentation of this file.
00001 /*! @file
00002   @brief
00003 
00004 
00005   <pre>
00006   Copyright (C) 2015 Kyushu Institute of Technology.
00007   Copyright (C) 2015 Shimane IT Open-Innovation Center.
00008 
00009   This file is distributed under BSD 3-Clause License.
00010 
00011 
00012   </pre>
00013 */
00014 
00015 #ifndef MRBC_SRC_VALUE_H_
00016 #define MRBC_SRC_VALUE_H_
00017 
00018 #include <stdint.h>
00019 #include "vm_config.h"
00020 
00021 #ifdef __cplusplus
00022 extern "C" {
00023 #endif
00024 
00025 
00026 // mrb types
00027 //typedef float mrb_float;
00028 typedef int32_t mrb_int;
00029 typedef int32_t mrb_sym;
00030 
00031 /* aspec access ? */
00032 #define MRB_ASPEC_REQ(a)          (((a) >> 18) & 0x1f)
00033 #define MRB_ASPEC_OPT(a)          (((a) >> 13) & 0x1f)
00034 #define MRB_ASPEC_REST(a)         (((a) >> 12) & 0x1)
00035 #define MRB_ASPEC_POST(a)         (((a) >> 7) & 0x1f)
00036 
00037 // #define GET_TYPE(v) ((v).tt)
00038 #define IS_FIXNUM(v) (((v).tt)==MRB_TT_FIXNUM)
00039 
00040 #pragma pack(2)
00041 
00042 //================================================================
00043 /*!@brief
00044 
00045 */
00046 typedef enum {
00047   /* internal use */
00048   MRB_TT_HANDLE = -1,
00049   /* primitive */
00050   MRB_TT_EMPTY = 0,
00051   MRB_TT_TRUE,
00052   MRB_TT_FALSE,
00053   MRB_TT_NIL,
00054   MRB_TT_FIXNUM,
00055   MRB_TT_FLOAT,
00056   MRB_TT_SYMBOL,
00057   /* non-primitive */
00058   MRB_TT_OBJECT = 20,
00059   MRB_TT_CLASS,
00060   MRB_TT_PROC,
00061   MRB_TT_ARRAY,
00062   MRB_TT_STRING,
00063   MRB_TT_RANGE,
00064   MRB_TT_HASH,
00065 
00066 } mrb_vtype;
00067 
00068 
00069 //================================================================
00070 /*!@brief
00071 
00072 */
00073 typedef struct RClass {
00074   struct RClass *next;  // linked list
00075   mrb_vtype tt:8;
00076   mrb_sym name;   // class name
00077   struct RClass *super;    // mrbc_class[super]
00078   struct RProc *procs;   // mrbc_proc[rprocs], linked list
00079 } mrb_class;
00080 
00081 
00082 //================================================================
00083 /*!@brief
00084 
00085 */
00086 typedef struct RObject {
00087   struct RObject *next;
00088   mrb_vtype tt;
00089   union {
00090     int32_t i;             // MRB_TT_FIXNUM
00091     struct RObject *obj;   // MRB_TT_OBJECT : link to object
00092     struct RClass *cls;    // MRB_TT_CLASS : link to class
00093     struct RProc *proc;    // MRB_TT_PROC : link to proc
00094     struct RObject *array; // MRB_TT_ARRAY : array of objects
00095     struct RObject *range; // MRB_TT_RANGE : link to range
00096     double d;              // MRB_TT_FLOAT : float
00097     char *str;             // MRB_TT_STRING : C-string
00098   } value;
00099 } mrb_object;
00100 typedef struct RObject mrb_value;
00101 
00102 
00103 struct VM;
00104 typedef void (*mrb_func_t)(struct VM *vm, mrb_value *v);
00105 
00106 
00107 
00108 //================================================================
00109 /*!@brief
00110 
00111 */
00112 typedef struct RProc {
00113   struct RProc *next;
00114   unsigned int c_func:1;   // 0:IREP, 1:C Func
00115   int16_t sym_id;
00116   union {
00117     struct IREP *irep;
00118     mrb_func_t func;
00119   } func;
00120 } mrb_proc;
00121 
00122 
00123 // alloc one object
00124 mrb_object *mrbc_obj_alloc(struct VM *vm, mrb_vtype tt);
00125 
00126 // alloc one class
00127 mrb_class *mrbc_class_alloc(struct VM *vm, const char *name, mrb_class *super);
00128 
00129 
00130 // alloc one RProc
00131 mrb_proc *mrbc_rproc_alloc(struct VM *vm, const char *name);
00132 mrb_proc *mrbc_rproc_alloc_to_class(struct VM *vm, const char *name, mrb_class *cls);
00133 
00134 // EQ two objects
00135 int mrbc_eq(mrb_value *v1, mrb_value *v2);
00136 
00137 
00138 // for C call
00139 #define SET_INT_RETURN(n)         {v[0].tt=MRB_TT_FIXNUM;v[0].value.i=(n);}
00140 #define SET_NIL_RETURN()          v[0].tt=MRB_TT_NIL
00141 #define SET_FALSE_RETURN()        v[0].tt=MRB_TT_FALSE
00142 #define SET_TRUE_RETURN()         v[0].tt=MRB_TT_TRUE
00143 #define SET_RETURN(n)             v[0]=n
00144 
00145 #define GET_TT_ARG(n)             v[(n)+1].tt
00146 #define GET_INT_ARG(n)            v[(n)+1].value.i
00147 #define GET_ARY_ARG(n)            v[(n)+1]
00148 #define GET_ARG(n)                v[(n)+1]
00149 #define GET_FLOAT_ARG(n)          v[(n)+1].value.d
00150 #define GET_STRING_ARG(n)          v[(n)+1].value.str
00151 
00152 #ifdef __cplusplus
00153 }
00154 #endif
00155 #endif
00156