亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? semantics.cpp

?? icarnegie 的作業 自己做的
?? CPP
?? 第 1 頁 / 共 2 頁
字號:
/* semantics -- */

#include "flincl.h"
#include "shortstack.h"
#include "semantics.h"
#include "stdio.h" // for compiler.h
#include "compiler.h"
#include "assert.h"

#define must(emit) if (!(emit)) return false;

typedef struct {
	char *name;
	short params;
	short op;
} Builtin_desc;

// built_ins are "global functions" that cannot be
// overridden or overloaded
Builtin_desc built_ins[] = {
	{ "array", 1, OP_NEWARRAY },
    { "dict", 1, OP_NEWDICT },
    { "flatten", 1, OP_FLATTEN },
    { "repr", 1, OP_REPR },
    { "str", 1, OP_STR },
    { "type", 1, OP_TYPE },
    { "len", 1, OP_LENGTH },
    { "cos", 1, OP_COS },
    { "sin", 1, OP_SIN },
    { "tan", 1, OP_TAN },
    { "exp", 1, OP_EXP },
    { "sqrt", 1, OP_SQRT },
    { "rem", 1, OP_REM },
    { "min", 2, OP_MIN },
    { "max", 2, OP_MAX },
    { "random", 0, OP_RANDOM },
    { "abs", 1, OP_ABS },
    { "int", 1, OP_TRUNCATE },
    { "round", 1, OP_ROUND },
    { "float", 1, OP_FLOAT },
    { "chr", 1, OP_CHR },
    { "ord", 1, OP_ORD },
    { "hex", 1, OP_HEX },
    { "id", 1, OP_ID },
    { "intern", 1, OP_INTERN },
    { "isinstance", 2, OP_INST },
    { "issubclass", 2, OP_SUBCL },
    { "oct", 1, OP_OCT },
    { "strcat", 2, OP_STRCAT },
    { "subseq", 3, OP_SUBSEQ },
    { "isupper", 1, OP_ISUPPER },
    { "islower", 1, OP_ISLOWER },
    { "isalpha", 1, OP_ISALPHA },
    { "isdigit", 1, OP_ISDIGIT },
    { "isalnum", 1, OP_ISALNUM },
    { "toupper", 1, OP_TOUPPER },
    { "tolower", 1, OP_TOLOWER },
    { "find", 4, OP_FIND },
	{ "exit", 0, OP_EXIT },
	{ "frame_previous", 1, OP_FRMPREV },
	{ "frame_variables", 1, OP_FRMVARS },
	{ "frame_pc", 1, OP_FRMPC },
	{ "frame_method", 1, OP_FRMMETH },
	{ "frame_class", 1, OP_FRMCLASS },
	{ "runtime_exception_nesting", 0, OP_NEST },
	{ "frame_get", 0, OP_FRMGET },
	{ NULL, 0, 0 }
};

// builtin_methods are implemented by op_codes,
// but if the first operand's type is object,
// a method lookup is performed. Builtin_methods
// allow us to implement builtin methods on builtin
// types, and run more efficiently than a full
// method call which allocates a frame.
Builtin_desc builtin_methods[] = {
    { "readline", 0, OP_READLINE },
    { "write", 1, OP_WRITE },
    { "read", 1, OP_READ },
	{ "unread", 1, OP_UNREAD },
    { "readvalue", 0, OP_READVAL },
    { "close", 0, OP_CLOSE },
    { "token", 0, OP_TOKEN },
	{ "append", 1, OP_APPEND },
	{ "unappend", 0, OP_UNAPPEND },
    { "last", 0, OP_LAST },
    { "keys", 0, OP_KEYS },
    { "values", 0, OP_VALUES },
    { "clear", 0, OP_CLEAR },
    { "index", 1, OP_INDEX },
    { "reverse", 0, OP_REVERSE },
    { "sort", 0, OP_SORT },
    { "remove", 1, OP_REMOVE },
    { "copy", 0, OP_COPY },
    { "has_key", 1, OP_HASKEY },
    { "get", 2, OP_GET },
	{ "value", 0, OP_VALUE },
    // { "get2", 2, OP_GET2 },
    { NULL, 0, 0 }
};



// builtin -- searches for id in built-in list
//
// if found, return op-code required to execute the built-in
// and set actuals to the number of parameters required by
// the built-in function
// otherwise, return 0 and set actuals to -1
//
short Semantics::builtin(char *id, short *actuals)
{
    int i = 0;
	while (built_ins[i].name) {
		if (streql(built_ins[i].name, id)) {
			*actuals = built_ins[i].params;
			return instr_compose(instr_op, built_ins[i].op);
		}
		i++;
	}
	*actuals = -1;
	return 0;
}


short Semantics::builtin_method(char *id, short *actuals)
{
    int i = 0;
    while (builtin_methods[i].name) {
        if (streql(builtin_methods[i].name, id)) {
            *actuals = builtin_methods[i].params;
            return instr_compose(instr_op, builtin_methods[i].op);
        }
        i++;
    }
    *actuals = -1;
    return 0;
}


bool Semantics::init(Machine_ptr m, Compiler_ptr c)
{
	mach = m;
	comp = c;
	instructions = NULL;
	if_stack = Short_stack::create(3, mach);
	loop_stack = Short_stack::create(2, mach);
	assign_stack = Short_stack::create(2, mach);
	indent_stack = Short_stack::create(8, mach);
	array_stack = Short_stack::create(2, mach);
	call_stack = Short_stack::create(2, mach);
    return if_stack && loop_stack && assign_stack && 
        indent_stack && array_stack && call_stack;
    // BUG: need error report here if returning false
}


Semantics::Semantics()
{
}


bool Semantics::reset()
{
    if (instructions) {
        //FLSys::free(instructions, max_instr);
    }
	class_started = false;
	method_started = false;
	local_offset = 0;
	member_offset = 0;
    next_instr = 0;
    max_instr = 0;
	method = NULL;
	fclass = NULL;
	method_name = NULL;
    class_name = NULL;
    var_name = NULL;
	array_index = 0;
	opcode = 0;
    actuals = 0;
	// initially has room for 16 symbols
	symbols = Array::create(0, 16, mach);
    return symbols != NULL;
}


bool Semantics::emit(short instruction)
{
    if (next_instr >= max_instr) {
        if (!grow()) return false;
    }
    if (next_instr < max_instr) {
        instructions->set_instr(next_instr++, instruction, 
                                mach->trace_flag);
    }
    return true;
}


bool Semantics::emit_at(long location, long instruction)
{
    assert(location < next_instr);
	if (instruction > 0x7FFF || instruction < -0x10000) {
		comp->report_error("virtual machine limitation: branch out of bounds");
		return false;
	}
    instructions->set_instr(location, (short) instruction, 
                            mach->trace_flag);
	return true;
}


bool Semantics::for_var(char *id)
{
	long index;  // variable index in frame
	// is this a local? look in method symbol table
	Symbol_ptr s = Symbol::create(id, mach);
    if (!method->get_localtable()->lookup(s, &index)) {
		comp->report_error("undeclared loop variable");
		return false;
	}
	// now index is the loop variable offset
	loop_stack->push((short) index, mach);
	return true;
}


bool Semantics::for_init()
{
	short index = loop_stack->pop(mach);
	// store the initial value
	must(emit(instr_compose(instr_store_local, (short) index)));
	loop_stack->push(index, mach);  // save for later
	return true;
}


bool Semantics::for_to()
{
	return true;
}


bool Semantics::for_by(bool by_flag)
{
	if (!by_flag) { // no BY expression
		emit(instr_compose(instr_load_int, 1));
	}
	// generate instruction to push the loopcounter
	short index = loop_stack->pop(mach);
	loop_stack->push(index, mach);
	emit(instr_compose(instr_load_local, (short) index));
	emit(OP_JUMP);
	loop_stack->push(next_instr, mach);
	emit(0);
	// machine stack is to_value, by_value, loopcount (top)
	return true;
}


// for_end_range -- called after for-=-to-by-:
//
bool Semantics::for_end_range()
{
	long looptop = loop_stack->pop(mach);
	long loopvar = loop_stack->pop(mach);
	return emit(OP_LOOPINC) &&
	       emit((short) loopvar) &&
		   // jump instruction is pc += *pc,
		   //    next_instr = looptop + *looptop,
		   //    *looptop = next_instr - looptop
		   emit_at(looptop, next_instr - looptop) &&
		   emit(OP_LOOPTST) &&
		   // jump instruction is pc += *pc
		   //    looptop + 1 = next_instr + *next_instr
		   //    *next_instr = looptop + 1 - next_instr
		   emit(looptop + 1 - next_instr) &&
	       // break instructions should branch to here
		   emit(OP_POP2);
}


bool Semantics::for_array()
{
	short index = loop_stack->pop(mach);
	must(emit(instr_compose(instr_load_int, 0)));
	must(emit(OP_JUMP));
	loop_stack->push(next_instr, mach);
	return emit(0) &&
		   // store the initial value
		   emit(instr_compose(instr_store_local, index));
}


bool Semantics::for_end_elements()
{
	// stack has looptop-1
	// machine stack has array index, then array
	long looptop = loop_stack->pop(mach);
	return emit_at(looptop, next_instr - looptop) &&
		   emit(OP_LOOPNXT) &&
		   emit(looptop - next_instr);
}


bool Semantics::return_null()
{
    emit(OP_RETNULL);
    return true;
}


// unemit -- undo the previous emit, and return opcode
//
short Semantics::unemit()
{
    if (next_instr <= 0) return OP_NOOP;
    next_instr--;
    return instructions->get_instr(next_instr);
}

    
bool Semantics::grow()
{
    short newlen = 128;
    if (newlen <= max_instr) newlen = max_instr * 2;
    assert(max_instr < newlen);
    Code_ptr newinstr = Code::create(newlen, mach);
    if (!newinstr) return false;
	if (next_instr > 0) {
        for (long i = 0; i < next_instr; i++) {
            newinstr->set_instr(i, instructions->get_instr(i), false);
		}
	}
    instructions = newinstr;
    max_instr = newlen;
    return true;
}


// call prepares to invoke a function.
// The current opcode,actuals pair is pushed
// The new opcode is looked up and saved until
// after parameters are ready
//
bool Semantics::call(char *id)
{
	call_stack->push(opcode, mach);
	call_stack->push(actuals, mach);
	opcode = builtin(id, &actuals);
	if (opcode == 0) {
	    long index = insert_symbol(id);
	    assert(index >= 0 && index < 2048);
	    emit(instr_compose(instr_local_frame, (short) index));
	}
	return true;
}


bool Semantics::method_call(char *id)
{
	call_stack->push(opcode, mach);
	call_stack->push(actuals, mach);
    opcode = builtin_method(id, &actuals);
    if (opcode == 0) {
	    long index = insert_symbol(id);
	    assert(index >= 0 && index < 2048);
	    opcode = instr_compose(instr_frame, (short) index);
    }
    return true;
}


bool Semantics::class_start(char *class_name, char *super_name)
{
	fclass = FClass::create(mach);
	if (!fclass) return false;
	Symbol_ptr s = Symbol::create(class_name, mach);
	mach->push(s);
	FClass_ptr probe = mach->class_lookup(s);
	if (probe) {
		comp->report_error("class is already defined");
		mach->pop();
		fclass = NULL;
		return false;
	}
	mach->class_add(s, fclass);
	mach->pop();
	if (super_name[0]) {
		s = Symbol::create(super_name, mach);
		FClass_ptr super = mach->class_lookup(s);
		if (!super) {
			comp->report_error("super class is undefined");
			fclass = NULL;
			return false;
		}
		fclass->init_super(super);
		member_offset = super->get_inst_slots();
	} else {
		member_offset = 1; // all objects have class pointer
	}
	class_started = true;
	return true;
}


bool Semantics::method_start()
{
    method_started = true;
    local_offset = 0;
    return true;
}


bool Semantics::method_end()
{
	emit(instr_compose(instr_op, OP_RETNULL));
	// copy the code vector to the method
    Code_ptr newinstr = Code::create(next_instr, mach);
    if (!newinstr) return false;
    for (long i = 0; i < next_instr; i++) {
        newinstr->set_instr(i, instructions->get_instr(i), false);
	}
    int len = 1;
    if (mach->trace_flag) {
        for (i = 0; i < next_instr; i += len) {
            char text[64];
            trace(newinstr->disassemble(i, newinstr->get_instr(i), 
                                        newinstr->get_instr(i + 1), text, 
                                        &len, method));
        }
    }
	method->set_code(newinstr, mach);
	method = NULL;
	method_started = false;
	next_instr = 0;
	return true;
}


bool Semantics::method_name_is(char *name)
{
	method_name = Symbol::create(name, mach);
    method = Method::create(method_name, mach);
    if (class_started) { // insert method in the class
		fclass->enter_method(method_name, method, mach);
		method->set_fclass(fclass, mach);
	} else { // attach method to symbol
        method_name->set_function(method, mach);
        method->set_fclass(NULL, mach);
    }
    // add symbols to method:
   	method->init_symbols(symbols, mach);
	if (mach->trace_flag)
        trace("method_name_is: symbol name is %s\n", 
		      method_name->get_name()->get_string());
    return true;
}


bool Semantics::begin_formals()
{
    formal_type = FORMAL_REQUIRED;
    return true;
}


bool Semantics::set_formal_type(short ft)
{
    if (ft < formal_type) {
    	comp->report_error("formal type out of order");
        return false;
    }
    formal_type = ft;
    return true;
}


bool Semantics::formal_is(char *name)
{
	// convert the name to a symbol in this class
	var_name = Symbol::create(name, mach);
    // position the stack above the parameter:
	method->set_stack_offset(1 + method->get_stack_offset());
    switch (formal_type) {
      case FORMAL_OPTIONAL:
        method->set_optionals(1 + method->get_optionals());
        // no break, fall through to increment parameters too!
      case FORMAL_REQUIRED:
        method->set_parameters(1 + method->get_parameters());
        break;
      case FORMAL_KEYWORD:
        method->set_keywords(1 + method->get_keywords());
        break;
      case FORMAL_REST:
        method->set_rest_flag(true);
        break;
      case FORMAL_DICTIONARY:
        method->set_dict_flag(true);
        break;
    }
        
    return method->get_localtable()->insert_var(var_name, local_offset++, mach);
}


bool Semantics::default_value(FVal v)
{
    // first see if there is an optionals array:
    Array_ptr defaults = method->get_default();
    if (!defaults) {
        defaults = Array::create(1, 1, mach);
        method->set_default(defaults);
    }
    // this new value is for local_offset-1, but the defaults start
    // at parameters - optionals
    long index = local_offset - 1 - 
        (method->get_parameters() - method->get_optionals());
    // make sure array is long enough:
    while (defaults->get_array_len() <= index) {
        defaults->append(NULL, mach);
    }
    defaults->set_fastref(index, v, mach);
    return true;
}


bool Semantics::end_of_formals()
{
    // signature is number of formals
    // stack_offset is number of formals + locals
	// frame_slots is stack_offset + maximum stack size
	method->set_frame_slots(method->get_stack_offset() + 
							EXPR_STACK_MAX);
    return true;
}


bool Semantics::id(char *id)
{
	Symbol_ptr s = Symbol::create(id, mach);
	long index; // variable index in frame
	Method_ptr mp;
	mach->push(s);
	// is this a local? look in method symbol table
    if (method->get_localtable()->lookup(s, &index)) {
		must(emit(instr_compose(instr_load_local, (short) index)));
	} else if (fclass &&
               fclass->get_symboltable()->lookup(s, &mp, &index) &&
			   mp == NULL) {
		must(emit(instr_compose(instr_load_instvar, (short) index)));
	} else { // load value from a global
		index = insert_symbol(id);
		must(emit(instr_compose(instr_load_global, (short) index)));
	}
	mach->pop();
	return true;
}


bool Semantics::assign_id(char *id)
{
	Symbol_ptr s = Symbol::create(id, mach);
	long index; // variable index in frame
	Method_ptr mp;

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美成人女星排名| 天堂久久一区二区三区| 久久久欧美精品sm网站| 日韩精品中文字幕在线不卡尤物| 欧美美女激情18p| 欧美日韩在线综合| 欧美日韩成人综合在线一区二区 | 欧美色图片你懂的| 日本精品免费观看高清观看| 91免费视频网| 在线观看日韩国产| 欧美日韩三级一区二区| 欧美女孩性生活视频| 欧美久久免费观看| 日韩视频国产视频| 久久久久久久一区| 国产精品毛片a∨一区二区三区| 国产精品久久久爽爽爽麻豆色哟哟| 中文字幕一区二区三中文字幕| 一区二区在线观看视频在线观看| 亚洲欧美视频一区| 亚洲高清在线精品| 蜜乳av一区二区三区| 国产精品69毛片高清亚洲| 成人av在线播放网站| 97se亚洲国产综合自在线观| 在线精品视频免费播放| 欧美色欧美亚洲另类二区| 91麻豆精品国产91久久久使用方法| 日韩一区二区三| 久久精品一区二区三区av | 激情综合色综合久久| 国产精品一区二区三区乱码| 99热在这里有精品免费| 欧美午夜片在线看| www成人在线观看| 国产精品理论片| 亚洲一区二区三区视频在线| 久久er精品视频| 91美女在线观看| 91精品综合久久久久久| 国产亚洲精品资源在线26u| 亚洲精品五月天| 日本欧美在线看| 成人黄色小视频| 制服丝袜中文字幕一区| 国产欧美精品区一区二区三区| 一区二区三区欧美久久| 经典三级在线一区| 91高清视频在线| 久久网站最新地址| 亚洲资源在线观看| 国产成人免费高清| 欧美亚一区二区| 久久久99精品久久| 亚洲r级在线视频| 国产白丝精品91爽爽久久| 欧美日韩国产高清一区二区| 欧美韩国日本综合| 日韩精彩视频在线观看| 粉嫩嫩av羞羞动漫久久久 | 午夜a成v人精品| 国产成人精品免费一区二区| 6080国产精品一区二区| 综合av第一页| 韩国一区二区三区| 欧美久久久久中文字幕| 亚洲色图一区二区三区| 韩国av一区二区三区在线观看| 欧美专区日韩专区| 中文字幕精品三区| 久久电影国产免费久久电影 | 久久影院电视剧免费观看| 亚洲一级在线观看| 成人视屏免费看| 精品粉嫩超白一线天av| 日本伊人色综合网| 在线观看亚洲a| 亚洲人成网站色在线观看| 国产自产高清不卡| 日韩一区二区高清| 日韩成人av影视| 色婷婷av一区二区三区gif| 中文字幕久久午夜不卡| 国模一区二区三区白浆| 日韩欧美一级精品久久| 日韩国产高清影视| 欧美日韩一卡二卡三卡| 一区二区三区产品免费精品久久75| 成人免费毛片app| 欧美经典一区二区三区| 国产九色精品成人porny| 日韩一区二区电影在线| 日韩 欧美一区二区三区| 欧美性极品少妇| 亚洲一区二区3| 欧美亚男人的天堂| 亚洲最大成人综合| 91豆麻精品91久久久久久| 亚洲色图.com| 色综合天天综合网天天看片| 亚洲色图第一区| 色综合久久中文字幕综合网| 亚洲欧美电影一区二区| 一本一道久久a久久精品| 亚洲人成7777| 欧美性感一区二区三区| 午夜日韩在线电影| 91精品国产高清一区二区三区| 亚洲福利一区二区三区| 欧美日韩亚洲综合在线 | 午夜精品久久久久久不卡8050| 日本二三区不卡| 亚洲国产精品影院| 欧美日韩国产a| 日韩国产欧美视频| xvideos.蜜桃一区二区| 国产高清精品久久久久| 国产精品国产三级国产专播品爱网| www.av亚洲| 亚洲第一在线综合网站| 日韩午夜精品电影| 国产ts人妖一区二区| 国产精品全国免费观看高清| 91色在线porny| 爽爽淫人综合网网站| 欧美电影精品一区二区| 国产精品一区二区三区99| 亚洲欧美一区二区在线观看| 91成人国产精品| 午夜精品久久一牛影视| 欧美成人一区二区三区片免费| 国产精品一区二区三区四区| 综合激情成人伊人| 91麻豆精品国产91久久久久久久久| 精品在线你懂的| 最新不卡av在线| 欧美一区二区日韩| 丰满亚洲少妇av| 亚洲成人资源网| 欧美精品一区在线观看| 91麻豆国产精品久久| 日韩高清中文字幕一区| 国产色婷婷亚洲99精品小说| 色8久久人人97超碰香蕉987| 蜜臀av性久久久久蜜臀aⅴ流畅 | 欧美大胆人体bbbb| 成人影视亚洲图片在线| 亚洲一级电影视频| 久久久亚洲精品一区二区三区| 色一区在线观看| 久久国产人妖系列| 综合久久给合久久狠狠狠97色 | 精品久久久久久无| 99vv1com这只有精品| 丝袜美腿亚洲一区| 中文字幕亚洲区| 欧美电影免费提供在线观看| 93久久精品日日躁夜夜躁欧美| 婷婷久久综合九色综合绿巨人| 久久精品免费在线观看| 欧美午夜精品电影| 成人午夜精品一区二区三区| 偷拍一区二区三区| 一区视频在线播放| 精品卡一卡二卡三卡四在线| 欧日韩精品视频| 国产999精品久久久久久绿帽| 亚洲一区在线视频观看| 欧美国产精品v| 日韩欧美精品在线| 在线日韩av片| 国产成人av一区二区三区在线| 午夜精品一区二区三区电影天堂| 成人欧美一区二区三区小说| 久久久影院官网| 欧美电影免费提供在线观看| 欧美天堂一区二区三区| 91免费精品国自产拍在线不卡| 国内成人精品2018免费看| 日本成人在线网站| 亚洲一区二区中文在线| 亚洲欧美一区二区不卡| 日本一区二区三区高清不卡 | 日韩av中文字幕一区二区| 亚洲精品你懂的| 亚洲欧美在线视频| 中文字幕精品一区| 久久久久久久综合色一本| 日韩欧美电影一区| 91 com成人网| 欧美日韩国产小视频在线观看| 91极品视觉盛宴| 色视频一区二区| 99在线精品观看| 99久久99久久精品免费观看| 大桥未久av一区二区三区中文| 经典三级在线一区| 激情五月激情综合网| 精品一区二区三区香蕉蜜桃|