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

📄 env.java

📁 本设计Tiger语言为源语言
💻 JAVA
字号:
package Semant;

import Symbol.*;

public class Env {
	Table venv=new Table();
	Table tenv=new Table();
	ErrorMsg.ErrorMsg errorMsg;
	Env(ErrorMsg.ErrorMsg err){
		errorMsg=err;
		//initialize venv and tenv with predefined identifiers
		
		//The primitive types in Tiger
		tenv.put(Symbol.symbol("int"),new Types.INT());
		tenv.put(Symbol.symbol("string"),new Types.STRING());
		tenv.put(Symbol.symbol("void"),new Types.VOID());
		tenv.put(Symbol.symbol("nil"),new Types.NIL());
		
		Types.Type ty_return;
		Types.RECORD ty_record0;
		Types.RECORD ty_record1;
		Types.RECORD ty_record2;		
		
		//function print(s : string)
		//Print the string on the standard output.
		ty_return=new Types.VOID();
		ty_record0=new Types.RECORD(Symbol.symbol("s"),(Types.Type)tenv.get(Symbol.symbol("string")),null);
		venv.put(Symbol.symbol("print"), new FunEntry(ty_record0,ty_return));
	    
	    //function printi(i : int)
	    //Print the integer on the standard output.
	    ty_return=new Types.VOID();
		ty_record0=new Types.RECORD(Symbol.symbol("i"),(Types.Type)tenv.get(Symbol.symbol("int")),null);
		venv.put(Symbol.symbol("printi"), new FunEntry(ty_record0,ty_return));
	    
	    //function flush()
	    //Flush the standard output buffer.
		ty_return=new Types.VOID();
		ty_record0=null;
		venv.put(Symbol.symbol("flush"), new FunEntry(ty_record0,ty_return));
		
		//function getchar() : string
		//Read and return a character from standard input; return an empty string at end-of-file.
		ty_return=new Types.STRING();
		ty_record0=null;
		venv.put(Symbol.symbol("getchar"), new FunEntry(ty_record0,ty_return));
		
		//function ord(s : string) : int
		//Return the ASCII value of the first character of s, or -1 if s is empty.
		ty_return=new Types.INT();
		ty_record0=new Types.RECORD(Symbol.symbol("s"),(Types.Type)tenv.get(Symbol.symbol("string")),null);
		venv.put(Symbol.symbol("ord"), new FunEntry(ty_record0,ty_return));
	    
	    //function chr(i : int) : string
	    //Return a single-character string for ASCII value i. Terminate program if i is out of range.
		ty_return=new Types.STRING();
		ty_record0=new Types.RECORD(Symbol.symbol("i"),(Types.Type)tenv.get(Symbol.symbol("int")),null);
		venv.put(Symbol.symbol("chr"), new FunEntry(ty_record0,ty_return));
	    
	    //function size(s : string) : int
	    //Return the number of characters in s.
		ty_return=new Types.INT();
		ty_record0=new Types.RECORD(Symbol.symbol("s"),(Types.Type)tenv.get(Symbol.symbol("string")),null);
		venv.put(Symbol.symbol("size"), new FunEntry(ty_record0,ty_return));
	    
	    //function substring(s:string,f:int,n:int):string
	    //Return the substring of s starting at the character f (first character is numbered zero) and going for n characters.
		ty_return=new Types.STRING();
		ty_record2=new Types.RECORD(Symbol.symbol("n"),(Types.Type)tenv.get(Symbol.symbol("int")),null);
		ty_record1=new Types.RECORD(Symbol.symbol("f"),(Types.Type)tenv.get(Symbol.symbol("int")),ty_record2);
		ty_record0=new Types.RECORD(Symbol.symbol("s"),(Types.Type)tenv.get(Symbol.symbol("string")),ty_record1);
		venv.put(Symbol.symbol("substring"), new FunEntry(ty_record0,ty_return));
	    
	    //function concat (s1:string, s2:string):string
	    //Return a new string consisting of s1 followed by s2.
		ty_return=new Types.STRING();
		ty_record1=new Types.RECORD(Symbol.symbol("s2"),(Types.Type)tenv.get(Symbol.symbol("string")),null);
		ty_record0=new Types.RECORD(Symbol.symbol("s1"),(Types.Type)tenv.get(Symbol.symbol("string")),ty_record1);
		venv.put(Symbol.symbol("concat"), new FunEntry(ty_record0,ty_return));
	    
	    //function not(i : int) : int
	    //Return 1 if i is zero, 0 otherwise.
		ty_return=new Types.INT();
		ty_record0=new Types.RECORD(Symbol.symbol("i"),(Types.Type)tenv.get(Symbol.symbol("int")),null);
		venv.put(Symbol.symbol("not"), new FunEntry(ty_record0,ty_return));
	    
	    //function exit(i : int)
	    //Terminate execution of the program with code i.
		ty_return=new Types.VOID();
		ty_record0=new Types.RECORD(Symbol.symbol("i"),(Types.Type)tenv.get(Symbol.symbol("int")),null);
		venv.put(Symbol.symbol("exit"), new FunEntry(ty_record0,ty_return));

	}
	
	public void report_error(int pos,String message) {
		errorMsg.error(pos, message);
	}
}

⌨️ 快捷键说明

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