⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c-smile.h

📁 C-smile OOL is a scripting language with C++-like grammar. It has compiler, VM running bytecodes and
💻 H
📖 第 1 页 / 共 2 页
字号:
  public:
    virtual void mark ();

    symbol_t      name;
    CLASS*        base;
    PACKAGE*      package;

    DICTIONARY *  members;

    int 		      instance_size;

    CODE *        ctor_function;
    CODE *        item_function;
    CODE *        dtor_function;
    CODE *        cast_function;

    CLASS ( const char *name,  CLASS *base, PACKAGE* package );

    virtual PACKAGE *
      get_package () 
    {
      return package;
    }

    virtual VALUE create_instance ();

    virtual
      operator VALUE () 
    {
      return VALUE ( this );
    }

    ENTRY add ( const char *name,  int type );
    ENTRY find ( const char *name );
    ENTRY find ( symbol_t sym );

    VALUE get ( unsigned int idx );
    int   get_type ( unsigned int idx );
    void  set ( unsigned int idx,  const VALUE& v );

    virtual size_t
      allocated_size () 
    {
      return sizeof ( CLASS );
    }

    string  full_name ();

    // theses functions return index of added item in members dictionary
    int 	add_function ( const char *name,  BUILTIN_FUNC *fcn );
    int 	add_static_function ( const char *name,  BUILTIN_FUNC *fcn );

    int 	add_property ( const char *name,  BUILTIN_FUNC *fcn );
    int 	add_static_property ( const char *name,  BUILTIN_FUNC *fcn );

    int 	add_static_data ( const char *name,  const VALUE& vd );
    int 	add_const ( const char *name,  const VALUE& vd );
    // this function returns index of the field in instance members array
    int   add_data ( const char *name, const VALUE& /*not used so far*/ );

    void 	check_name ( symbol_t ns,  CODE *c,  int symbol_type );

    virtual CLASS * get_class ();

    virtual VALUE 	to_string ( const VALUE *v );

    virtual THING *
      load ( archive *arc ) 
    {
      return 0;
    };

    virtual bool
      save ( THING *me,  archive *arc ) 
    {
      return false;
    };

    friend class archive;

  };


  class OBJECT: public THING
  {
    friend class CLASS;
  protected:

  public:
    virtual void mark ();

    CLASS *   klass;
    void  *   tag;
    VALUE     members [ 1 ];

    OBJECT ( CLASS *cls );

    virtual size_t
      allocated_size () 
    {
      return sizeof ( OBJECT ) + ( klass->instance_size - 1 ) * sizeof ( VALUE );
    }

    virtual
      operator VALUE () 
    {
      return VALUE ( this );
    }

    virtual CLASS * get_class ();

    VALUE
      get ( unsigned int idx ) 
    {
      assert ( idx < ( unsigned int ) klass->instance_size );
      return members [ idx ];
    }

    void
      set ( unsigned int idx,   const VALUE& v ) 
    {
      assert ( idx < ( unsigned int ) klass->instance_size );
      members [ idx ] = v;
    }


  };


  class ARRAY : public THING
  {
  public:

  protected:
    ARRAY () 
    {
    }

  public:
    array<VALUE> data;

  public:
    virtual void mark ();

    ARRAY ( int n );

    virtual size_t
      allocated_size () 
    {
      return sizeof ( ARRAY );
    }

    int
      size () const
    {
      return data.size ();
    }

    void    size ( int newsize );

    VALUE&        operator [ ] ( int i );
    const VALUE&  operator [ ] ( int i ) const;

    int     push ( const VALUE& v );
    VALUE   pop ();

    virtual
      operator VALUE () 
    {
      return VALUE ( this );
    }

    virtual CLASS * get_class ();

    void      sort ( void *param,  bool ( *less ) ( void *param,  VALUE& a, VALUE& b ) );
    ARRAY *   slice ( int start, int end );
    void      remove ( int start, int end );

  protected:
    void sort ( void *param,  bool ( *less ) ( void *param,  VALUE& a, VALUE& b ),  int lo,  int hi );
  };


  class STRING: public THING
  {
    string _data;

    //protected:
  public:

    //just use default:  virtual void mark ();

    STRING () 
    {
    }

    STRING ( int l ) : _data ( ' ', l ) 
    {
    }

    STRING ( const char *str ) : _data ( str ) 
    {
    }

    STRING ( const string& str ) : _data ( str ) 
    {
    }

    virtual size_t
      allocated_size () 
    {
      return sizeof ( STRING );
    }

    virtual size_t
      size () const
    {
      return _data.length ();
    }

    char
      operator () ( int i ) const
    {
      return _data [ i ];
    }

    char &
      operator [ ] ( int i ) 
    {
      return _data [ i ];
    }

    STRING * slice ( int start, int end );

    void
      clear () 
    {
      _data.clear ();
    };

    virtual
      operator VALUE () 
    {
      return VALUE ( this );
    }

    virtual CLASS *get_class ();

    const string &
      cstr () const
    {
      return _data;
    }

    STRING
      operator+ ( const STRING &s ) const
    {
      return STRING ( _data + s._data );
    };

    bool
      operator< ( const STRING &s ) const
    {
      return _data < s._data;
    }

    bool
      operator<= ( const STRING &s ) const
    {
      return _data <= s._data;
    }

    bool
      operator == ( const STRING &s ) const
    {
      return _data == s._data;
    }

    bool
      operator != ( const STRING &s ) const
    {
      return _data != s._data;
    }

  };

#define CSTR(ps) ps->cstr () 

  class DICTIONARY: public THING
  {
    friend class CLASS;
  public:
    struct item
    {
      int       type;
      symbol_t  symbol;
      VALUE     value;

      void init () 
      {
        type = 0;
        symbol = undefined_symbol;
        value.init ();
      }
    };
  protected:
    DICTIONARY () : _size ( 0 ),  _allocated_size ( 0 ) 
    {
    }

    int   _size;
    int   _allocated_size;
    item  _items [ 1 ];

  public:
    virtual void mark ();

    static DICTIONARY* create ( int size = 0 );
    DICTIONARY * realloc ( int newsize );

    int
      find ( symbol_t sym ) 
    {
      for ( int i = 0; i < _size; i++ )
        if ( _items [ i ].symbol == sym )
          return i;
      return -1;
    }

    DICTIONARY::item &
      operator [] ( int idx ) 
    {
      return _items [ idx ];
    }

    const DICTIONARY::item &
      operator [ ] ( int idx ) const
    {
      return _items [ idx ];
    }

    DICTIONARY::item &
      get ( int idx ) 
    {
      return _items [ idx ];
    }

    int
      size () const
    {
      return _size;
    }

    virtual size_t
      allocated_size () 
    {
      return sizeof ( DICTIONARY ) + ( _allocated_size - 1 ) * sizeof ( item );
    }

    virtual
      operator VALUE () 
    {
      assert ( false );
      return VALUE ();
    }

    virtual CLASS * get_class ();

    VALUE
      find_value ( symbol_t sym ) 
    {
      int idx = find ( sym );
      if ( idx >= 0 ) 
        return get ( idx ).value;
      return VALUE ();
    }
  };

  typedef array<unsigned char> BUFFER;

  class CODE : public THING
  {
  protected:
    union
    {
      BUFFER *        bc;
      BUILTIN_FUNC *  native;
    }
    _code;
    bool      _is_native;
    CLASS *   _klass;
    symbol_t  _name;

    CODE () : _klass ( 0 ), _name ( undefined_symbol )
    {
      _code.bc = 0;
    }

  public:
    virtual void mark ();

    CODE ( symbol_t  name,  BUFFER *bytecode,  CLASS *klass );
    CODE ( symbol_t  name,  BUILTIN_FUNC *native,  CLASS *klass );

    virtual size_t
      allocated_size () 
    {
      return sizeof ( *this );
    }

    BUILTIN_FUNC *
      native () 
    {
      assert ( _is_native );
      return _code.native;
    }

    const unsigned char *
      bytecode () 
    {
      assert ( !_is_native );
      return & ( *_code.bc ) [ 0 ];
    }

    int
      bytecode_size () 
    {
      assert ( !_is_native );
      return _code.bc->size ();
    }

    CLASS *
      klass () 
    {
      return _klass;
    }

    void
      klass ( CLASS *c ) 
    {
      _klass = c;
    }

    symbol_t
      name () 
    {
      return _name;
    }

    bool
      is_bytecode () const
    {
      return !_is_native;
    }

    bool
      is_native () const
    {
      return _is_native;
    }

    string  full_name ();

    virtual
      operator VALUE () 
    {
      return VALUE ( this );
    }

    virtual CLASS * get_class ();

    friend class archive;
    friend class CLASS;

  };


  //external variables

  /* rtl.cpp */
  void	print1 ( VM *vm, VALUE *ios, int qflag, VALUE *val );

  class PACKAGE: public CLASS
  {
  protected:

    PACKAGE () : literals ( 0 ),  file_name ( 0 ),  init_code ( 0 ) 
    {
    }

  public:
    virtual void mark ();

    ARRAY *       literals;
    STRING *      file_name;
    CODE *        init_code; // native packages do not have init_code

    PACKAGE ( const char *name,  const char *file_name = 0 );
    PACKAGE ( symbol_t name );

    virtual PACKAGE *
      get_package () 
    {
      return this;
    }

    virtual size_t
      allocated_size () 
    {
      return sizeof ( PACKAGE );
    }

    int add_literal ( symbol_t vs );
    int add_literal ( const VALUE &v );
    int add_literal ( const char *s );

    //int add_package_reference ( PACKAGE *pkg ) { VALUE v = pkg; return add_literal ( v ); };

    virtual void
      add_function ( const char *name,   BUILTIN_FUNC *fcn ) 
    {
      assert ( 0 );
    }

    virtual void
      add_property ( const char *name,   BUILTIN_FUNC *fcn ) 
    {
      assert ( 0 );
    }

    virtual
      operator VALUE () 
    {
      return VALUE ( (  CLASS * ) this );
    }

    bool
      has_reference_to ( symbol_t package_symbol );

    bool
      is_native () 
    {
      return init_code == 0;
    }
  };


  int cs_main ( int argc,  char *argv [ ] );

  void    error_parameters ();
  void    error_read_only ();

};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -