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

📄 type.c

📁 一个编译器修改的例子
💻 C
字号:
#include "error.h"#include "salloc.h"#include "type.h"#include <assert.h>Type *new_simple_type(Type_subtype subtype){    Type *type		= safe_malloc(sizeof(Type));    type->subtype	= subtype;    type->ref_count	= 1;    type->call_by_ref	= false;    return type;}Type *new_array_type(Type *base_type){    Type *type			= safe_malloc(sizeof(Type));    type->subtype		= array_type;    type->ref_count		= 1;    type->call_by_ref		= false;    ARRAY_TYPE(type).base_type	= base_type;    return type;}Type *new_null_type(void){    Type *type			= safe_malloc(sizeof(Type));    type->subtype		= null_type;    type->ref_count		= 1;    type->call_by_ref		= false;    return type;}Type *new_subprog_type(Declaration *decl){    Type *type			= safe_malloc(sizeof(Type));    type->subtype		= subprog_type;    type->ref_count		= 1;    type->call_by_ref		= false;    SUBPROG_TYPE(type).decl	= decl;    return type;}/*add : create a new record type */Type *new_record_type(Declaration *decl){    Type *type =safe_malloc(sizeof(Type));    type->subtype  =record_type;    type->ref_count =1;    type->call_by_ref  =false;    RECORD_TYPE(type).decl=decl;    return type;}void inc_ref_count(Type *type, int n){    if (type != 0)	type->ref_count += n;}void delete_type(Type *type){    if (type != 0 && -- type->ref_count == 0)    {	switch (type->subtype)	{	    case array_type:		delete_type(ARRAY_TYPE(type).base_type);		break;	    default:		break;	}	free(type);    }}Bool types_compatible(const Type *t1, const Type *t2){    if (t1 == 0 || t2 == 0) {	assert(error_seen);	return true;    }    switch (t1->subtype)    {	case array_type:	     return t2->subtype == null_type ||		    (t2->subtype == array_type && types_compatible(		    ARRAY_TYPE(t1).base_type, ARRAY_TYPE(t2).base_type));	case null_type:	     return (t2->subtype & reference_type) != 0;	default:	     return t1->subtype == t2->subtype;    }}/* add : generate C code for new record type */void generate_type(FILE *file, Type *type){    switch (type->subtype)    {	case char_type:	    fprintf(file, "char"); 	    break;	case bool_type:	case int_type:	    fprintf(file, "int"); 	    break;	case real_type:	    fprintf(file, "double"); 	    break;	case array_type:	    fprintf(file, "array");	    break;	case void_type:	    fprintf(file, "void");	    break;      case record_type:          fprintf(file,"record_%s",RECORD_TYPE(type).decl->token->name);          break;	default:	    assert(false);    }    if (type->call_by_ref)	putc('*', file);}

⌨️ 快捷键说明

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