📄 module.h
字号:
#ifndef __module_h
#define __module_h
///////////////////////////////////////////////////////////////////////////////
// $Header: /shorthand/src/module.h 4 1/09/03 7:14p Arm $
//-----------------------------------------------------------------------------
// Project: ShortHand interpreter
// Author: Andrei Remenchuk <andrei@remenchuk.com>
//-----------------------------------------------------------------------------
// module.h: definition of ShortHand Module - highest-level execution unit
///////////////////////////////////////////////////////////////////////////////
#include "array.h"
#include "map.h"
#include "yydef.h"
#include "nodes.h"
#include "http.h"
/**
* ShortHand execution context.
*/
class ShhContext
{
protected:
/** script variables */
ShhVariableList m_variables;
/** objects pool (no bindings are stored here) */
array<ShhObject> m_objects;
/** GET variables */
cchar_map m_get_fields;
/** PUT variables */
cchar_map m_put_fields;
/** A list of modules that have already been included */
string_array m_included_modules;
/** memory pool (included modules user parent's pool) */
memory m_memory;
friend class ShhModule;
};
class ShhModule;
class ShhModuleList : public array<ShhModule>
{
public:
ShhModuleList() {}
};
/**
* ShortHandle module class.
*/
class ShhModule
{
protected:
/**
* Module's own context (may be never used for included modules)
*/
ShhContext m_own_context;
/**
* Actual context - may point either to own context, or to the parent context,
* but may never be NULL.
*/
ShhContext* m_context;
/** Parent module (if this module is inclusion or successor) */
ShhModule* m_parent;
/** parsed syntax tree */
array<ShhNode> m_nodes;
/** module entry point */
ShhExecutable* m_entry;
/** full name of the script source file */
string m_script_name;
/** fallback HTTP stream (may never be used, but it doesn't take significant space) */
StandardHttpStream m_stdio_stream;
/** user-defined functions */
ShhDefunList m_ufds;
/** actual HTTP stream */
HttpStream* m_http_stream;
/**
* Function call parameters and local variables; This points to the variables list
* when the module is executing UDF. At all other times
* this member varialble has NULL value.
*/
ShhVariableList* m_locals;
/**
* A pointer to the return value slot when executing UDF
*/
ShhValue* m_return_value;
ShhModuleList m_modules;
public:
/** current loop */
ShhLoop* m_current_loop;
/** Lexer */
ShortHandLexer* m_lexer;
protected:
void parse_vars_into_map(char* source, cchar_map& target);
void parse_query_string();
void parse_posted_data();
void execute_body();
int parse_script(const char* content);
public:
// globals replacememts for Yacc-generated parser
int module_yylex(void* yylval, void* yylloc);
int module_yyerror(char* s);
// assigns entry point
void setEntry(ShhExecutable* node) { m_entry = node; }
public:
// Constructs new module for the given script and parent context.
ShhModule(const char* script_name, ShhModule* parent_module = NULL);
ShhModule* top();
ShhVariable* findVariable(const char* name);
// attaches node to the current module
ShhNode* include(ShhNode* node);
ShhDefunList& getFunctionList();
// clears execution environment
void clear();
// return full script name
const char* getName() const { return m_script_name; }
// provides scratch memory
memory& scratch() { return m_context->m_memory; }
ShhExecutable* getEntry() const { return m_entry; }
// provides variable (finds existing or creates new one)
ShhVariable* var(const char* name);
// constructs an object
ShhObject* createObject(const char* type, const YYLTYPE& location);
// tries to execute named function
ShhValue executeFunction(const char* name, const ShhValueList& args);
// gets value of GET or POST variable (in there's no GET)
const char* getField(const char* name);
// gets value of POST variable
const char* POST(const char* name);
// gets value of GET variable
const char* GET(const char* name);
/**
* Executes another module in the current context.
*
* parameters:
* location: location where another module was mentioned
* name: name of the module to include
* do_return: indicates whether or not we should return
* after finishing with another module
* flags: reserved
*/
void switch_module(ShhNode& location, const char* name, bool do_return, int flags = 0);
int vprintf(const char* format, va_list args);
int printf(const char* format, ...);
int puts(const char* s);
HttpStream* stream() { return m_http_stream; }
int execute_script(const char* content, HttpStream* http_stream = NULL);
int execute_file(FILE* file);
~ShhModule();
friend class ShhDefun;
friend class ShhReturn;
friend class ShhLocalDecl;
};
class ShhExitException
{
protected:
ShhValue m_value;
public:
ShhExitException() {}
friend class ShhModule;
};
class ShhReturnException
{
public:
ShhReturnException(){}
};
#endif //__module_h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -