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

📄 c-smile.h

📁 C-smile OOL is a scripting language with C++-like grammar. It has compiler, VM running bytecodes and
💻 H
📖 第 1 页 / 共 2 页
字号:
/*
*
* c-smile.h
*
* Copyright (c) 2001, 2002
* Andrew Fedoniouk - andrew@terra-informatica.org
* Portions: Serge Kuznetsov - kuznetsov@deeptown.org
*
* See the file "COPYING" for information on usage 
* and redistribution of this file
*
*/

#ifndef __cs_H
#define __cs_H

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "tool.h"
#include "sym_table.h"

namespace c_smile
{

#define BANNER    ("c-smile v1.0 - Copyright (c) 2001-2002,  by Andrew Fedoniuk") 
#define MAGIC     ("c-smileC") 
#define VERSION   ("1000") 

  // limits
#define SMAX		1000	          // runtime stack size
#define ERROR_HANDLER_SMAX 64   // try catch stack size
#define SYNCHRO_SMAX 32         // synchonized stack size

  // useful definitions
#define TRUE		1
#define FALSE		0

  class archive;

#define set_null(s) ( (s)->v_type = DT_NULL ) 

  class THING;
  class CLASS;
  class OBJECT;
  class ARRAY;
  class STRING;
  class IOSTREAM;
  class CODE;

  class io_stream;

  // data types
#define _DTMIN		        0

#define DT_NULL		        0
#define DT_STRING         1
#define DT_FLOAT	      	2
#define DT_INTEGER	    	3

#define DT_CLASS	        4
#define DT_OBJECT	        5
#define DT_ARRAY	        6

#define DT_CODE			      7

#define DT_VAR		        8

#define DT_EXT		      	9
#define DT_SYMBOL	        10

#define DT_OBJECT_METHOD	11

#define _DTMAX			      11

  // symbol types
#define ST_CLASS      1		// class definition
#define ST_DATA       2		// data member
#define ST_SDATA      3		// static data member
#define ST_FUNCTION	  4		// function member
#define ST_SFUNCTION  5		// static function member
#define ST_PROPERTY	  6		// property function member
#define ST_SPROPERTY  7		// static property function member
#define ST_CONST      8		// static property function member

  class VALUE;
  class VM;
#define mark_thing(th) if ( ( th ) && ! ( th )->marked () )  ( th )->mark ();
  //used to be: void _mark_thing_ ( THING **th );

  typedef VALUE ( BUILTIN_FUNC ) ( int argc, VALUE *argv );

  class VM;

  class THING
  {
  protected:
    // block flags
#define MARK	1

  public:
    byte    flags;

    bool
      marked () 
    {
      return ( flags & MARK ) != 0;
    }

    void
      clear_mark () 
    {
      flags = 0;
    }

    THING () : flags ( 0 ) 
    {
    }

    virtual
      ~THING () 
    {
    }

    virtual size_t allocated_size () = 0;

    void* operator new ( size_t size );
    void* operator new ( size_t size, int moresize );
    void  operator delete ( void* p );
    void  operator delete ( void* p, int moresize );

    virtual void
      mark () 
    {
      flags |= MARK;
    }

    virtual operator VALUE ();

    virtual CLASS *get_class () = 0;
    virtual bool instance_of ( CLASS *cls );

    virtual io_stream *
      get_stream () 
    {
      return 0;
    }
    // create stream based on this object ( if possible ) 

  };



  // member of class reference
  struct ENTRY
  {
    int     index;
    CLASS*  klass;

    VALUE *   value ();
    symbol_t 	symbol () const;
    symbol_t& symbol ();
    int&      type ();

    bool is_valid ();

    void mark ();

    static ENTRY
      undefined () 
    {
      ENTRY e;
      e.index = 0;
      e.klass = 0;
      return e;
    }

  };

#pragma pack ( push, 2 ) 

  // value descriptor structure
  class VALUE
  {

  public:
    struct OBJECT_METHOD
    {
      THING * thing;
      CODE  * code;
      void    mark ();
    };
  public:
    short int       v_type;    // data type
    union V
    {
      // value
      // gc things
      CLASS *       v_class;		  //   klass
      OBJECT *      v_object;		  //   object
      ARRAY *       v_vector;		  //   vector
      STRING *      v_string;     //   string

      THING *       v_thing;		  //   DT_EXT
      CODE *        v_code;	      //

      ENTRY         v_var;        //   class.variable reference
      OBJECT_METHOD v_om;         //   object.method reference

      // not gc things
      symbol_t          v_symbol;     //   symbol ( id ) 
      int			          v_integer;	  //   integer
      double	          v_float;	    //   float
      qword				      data;	        //   all data

    }
    v;


    VALUE () : v_type ( DT_NULL ) 
    {
      v.data = 0;
    }
    VALUE ( int i ) : v_type ( DT_INTEGER ) 
    {
      v.data = 0;
      v.v_integer = i;
    }
    VALUE ( long l ) : v_type ( DT_INTEGER ) 
    {
      v.data = 0;
      v.v_integer = l;
    }
    VALUE ( double d ) : v_type ( DT_FLOAT ) 
    {
      v.data = 0;
      v.v_float = d;
    }
    VALUE ( bool b ) : v_type ( DT_INTEGER ) 
    {
      v.data = 0;
      v.v_integer = b ? TRUE : FALSE;
    }

    VALUE ( const char *c );
    VALUE ( const string& s );

    VALUE ( ENTRY& de ) : v_type ( DT_VAR ) 
    {
      v.data = 0;
      v.v_var = de;
    }

    VALUE ( OBJECT *ob ) : v_type ( DT_OBJECT ) 
    {
      v.data = 0;
      v.v_object = ob;
    }
    VALUE ( CLASS *cl ) : v_type ( DT_CLASS ) 
    {
      v.data = 0;
      v.v_class = cl;
    }
    VALUE ( ARRAY *ve ) : v_type ( DT_ARRAY ) 
    {
      v.data = 0;
      v.v_vector = ve;
    }
    VALUE ( STRING *st ) : v_type ( DT_STRING ) 
    {
      v.data = 0;
      v.v_string = st;
    }
    VALUE ( THING *s ) : v_type ( DT_EXT ) 
    {
      v.data = 0;
      v.v_thing = s;
    }

    VALUE ( const VALUE& copy ) : v_type ( copy.v_type ) 
    {
      v.data = copy.v.data;
    }

    VALUE &
      operator= ( const VALUE& copy ) 
    {
      init ();
      v_type = copy.v_type;
      v.data = copy.v.data;
      return *this;
    }

    VALUE &
      operator= ( CLASS *cl ) 
    {
      init (); if ( cl ) 
      {
        v_type = DT_CLASS; v.v_class = cl;
      }
      return *this;
    }

    VALUE &
      operator= ( ARRAY *ve ) 
    {
      init (); if ( ve ) 
      {
        v_type = DT_ARRAY; v.v_vector = ve;
      }
      return *this;
    }

    VALUE &
      operator= ( STRING *st ) 
    {
      init (); if ( st ) 
      {
        v_type = DT_STRING; v.v_string = st;
      }
      return *this;
    }

    VALUE &
      operator= ( OBJECT *ob ) 
    {
      init (); if ( ob ) 
      {
        v_type = DT_OBJECT; v.v_object = ob;
      }
      return *this;
    }

    VALUE &
      operator= ( THING *ex ) 
    {
      init ();
      if ( ex ) 
      {
        v_type = DT_EXT; v.v_thing = ex;
      }
      return *this;
    }

    VALUE &
      operator= ( CODE *bc ) 
    {
      init ();
      if ( bc ) 
      {
        v_type = DT_CODE; v.v_code = bc;
      }
      return *this;
    }

    VALUE &
      operator= ( int i ) 
    {
      init ();
      v_type = DT_INTEGER;
      v.v_integer = i;
      return *this;
    }

    VALUE &
      operator= ( long i ) 
    {
      init ();
      v_type = DT_INTEGER;
      v.v_integer = i;
      return *this;
    }

    VALUE &
      operator= ( double d ) 
    {
      init ();
      v_type = DT_FLOAT;
      v.v_float = d;
      return *this;
    }

    VALUE &
      operator= ( bool b ) 
    {
      init ();
      v_type = DT_INTEGER;
      v.v_integer = b?TRUE:FALSE;
      return *this;
    }

    void
      set_symbol ( symbol_t sym ) 
    {
      init ();
      v_type = DT_SYMBOL;
      v.v_symbol = sym;
    }

    void
      set_method ( THING *th, CODE *method ) 
    {
      init ();
      v_type = DT_OBJECT_METHOD;
      v.v_om.thing = th;
      v.v_om.code = method;
    }

    void mark ();

    void
      init () 
    {
      v_type = DT_NULL;
      v.data = 0;
    }

    bool
      is_null () const
    {
      return v_type == DT_NULL;
    }

    bool
      is_thing () const
    {
      return ( v_type == DT_CLASS || v_type == DT_OBJECT ||
               v_type == DT_ARRAY || v_type == DT_CODE ||
               v_type == DT_STRING || v_type == DT_EXT );
    }

    bool
      is_number () const
    {
      return ( v_type == DT_INTEGER || v_type == DT_FLOAT || v_type == DT_NULL );
    }

    bool
      is_string () const
    {
      return ( v_type == DT_STRING );
    }

    bool
      is_int () const
    {
      return ( v_type == DT_INTEGER );
    }

    bool
      is_float () const
    {
      return ( v_type == DT_FLOAT );
    }

    bool
      is_array () const
    {
      return ( v_type == DT_ARRAY );
    }

    bool
      is_class () const
    {
      return ( v_type == DT_CLASS );
    }

    bool
      is_object () const
    {
      return ( v_type == DT_OBJECT );
    }

    bool is_bytecode () const;
    bool is_nativecode () const;

    bool
      is_method () const
    {
      return ( v_type == DT_OBJECT_METHOD );
    }

    bool
      is_code () const
    {
      return ( v_type == DT_CODE ) || ( v_type == DT_OBJECT_METHOD );
    }

    bool
      is_ext_object () const
    {
      return ( v_type == DT_EXT );
    }

    operator int () const
    {
      if ( v_type == DT_INTEGER )
        return v.v_integer;
      else if ( v_type == DT_FLOAT )
        return int ( v.v_float );
      else
        return 0;
    }

    operator double () const
    {
      if ( v_type == DT_INTEGER )
        return double ( v.v_integer );
      else if ( v_type == DT_FLOAT )
        return v.v_float;
      else
        return 0.0;
    }

    operator bool () const;

    operator  string () const;    // may call bytecode
    STRING *  to_STRING () const; // may call bytecode
    string    to_string () const; // will not call any bytecode

    THING *   ext ( CLASS *klass ) const;

  };

#pragma pack ( pop ) 

  bool operator == ( const VALUE& vl, const VALUE& vr );


  class DICTIONARY;
  class PACKAGE;

  class archive;

  class CLASS: public THING
  {

  protected:

    CLASS () : instance_size ( 0 ), name ( undefined_symbol ), base ( 0 ),
               members ( 0 ), package ( 0 ), 
               ctor_function ( 0 ), 
               item_function ( 0 ), 
               dtor_function ( 0 ), 
               cast_function ( 0 ) 
    {
    }

    CLASS ( symbol_t t ) : instance_size ( 0 ), name ( t ), base ( 0 ),
                           members ( 0 ), package ( 0 ), 
                           ctor_function ( 0 ), 
                           item_function ( 0 ), 
                           dtor_function ( 0 ), 
                           cast_function ( 0 ) 
    {
    }

⌨️ 快捷键说明

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