首页 > 其他分享 >Lut语言

Lut语言

时间:2023-07-22 16:22:14浏览次数:29  
标签:opt Opt Lut 语言 maps exps int name

Lut语言与Lit语言差不多,将Lit语言的后缀表达式换成普通的中缀表达式,但删去了有关数组的内容。

解释器:

#include<bits/stdc++.h>
#define pt st.top();st.pop()
using namespace std;
map<string,int>mp,lab;
namespace expr{
	struct Opt{
		char name;
		int stackInJb,stackOutJb;
		Opt(char name,int in,int out){
			this->name=name;
			this->stackInJb=in;
			this->stackOutJb=out;
		}
		bool cmp(Opt*opt){
			return this->stackOutJb>opt->stackInJb;
		}
	};
	map<char,Opt*>maps;
	void mapOpt(){
		maps['%']=new Opt('%',2,2);
		maps['/']=new Opt('/',2,2);
		maps['*']=new Opt('*',2,2);
		maps['+']=new Opt('+',1,1);
		maps['-']=new Opt('-',1,1);
		maps['(']=new Opt('(',0,4);
		maps[')']=new Opt(')',-1,-1);
	}
	int calc(string s){
		mapOpt();
		stack<int>numStack;
		stack<char>optStack;
		char exps[20]="(";
		strcat(exps,s.c_str());
		exps[strlen(exps)]=')';
		optStack.push(exps[0]);
		Opt*opt,*opt_;
		for(int i=1;exps[i];){
			if(isalpha(exps[i])){
				string names="";
				while(isalpha(exps[i]))names+=exps[i],i++;
				numStack.push(mp[names]);
			}else if(!isdigit(exps[i])){
				opt=maps[optStack.top()];
				opt_=maps[exps[i]];
				if(opt_->name==')'&&opt->name=='('){
					optStack.pop();
					i++;
					continue;
				}
				bool com=opt_->cmp(opt);
				if(com){
					optStack.push(opt_->name);
					i++;
				}else{
					char n=opt->name;
					optStack.pop();
					int res,optNum1=numStack.top();
					numStack.pop();
					int optNum2=numStack.top();
					numStack.pop();
					if(n=='*')
						res=optNum2*optNum1;
					else if(n=='+')
						res=optNum2+optNum1;
					else if(n=='-')
						res=optNum2-optNum1;
					else if(n=='/')
						res=optNum2*optNum1;
					else if(n=='%')
						res=optNum2%optNum1;
					numStack.push(res);
				}
			}else{
				int num=0;
				while(isdigit(exps[i]))num=num*10+exps[i]-48,i++;
				numStack.push(num);
			}
		}	
		return numStack.top();
	}
}
using namespace expr;
int cnt,nu[10005],pos,p;
string s[1005];
void run(int x){
	for(int i=x;i<=cnt;i++){
		if(s[i].substr(0,3)=="def"){
			string t="";
			for(int j=4;j<s[i].size();j++){
				if(s[i][j]!=' ')t+=s[i][j];
				if(s[i][j]==' '||j==s[i].size()-1)mp[t]=0,t="";
			}
		}
		else if(s[i].substr(0,5)=="print"){
			if(s[i][5]==' ')cout<<calc(s[i].substr(6));
			else if(s[i][5]=='\"')cout<<s[i].substr(6);
			else cout<<(char)calc(s[i].substr(6));
		}
		else if(s[i].substr(0,4)=="scan"){
			mp[s[i].substr(5)]=nu[++p];
		}
		else if(s[i].substr(0,2)=="eq"){
			string t="";int j=3;
			for(;s[i][j]!=' ';j++)t+=s[i][j];
			mp[t]=calc(s[i].substr(j+1));
		}
		else if(s[i].substr(0,4)=="goto"){
			run(lab[s[i].substr(5)]+1);
			return;
		}
		else if(s[i].substr(0,2)=="if"){
			string t=s[i].substr(3);
			int ppp=calc(t),j=i+1;
			for(;s[j]!="endf";j++);
			if(ppp)run(i+1);
			else run(j+1);
			return;
		}
	}
}
int main(){
	puts("--------<Lut Programming Language>--------");
	puts("Thank you for using this Programming Language!");
	puts("It\'s a little Programming Language, It\'s name is Lut.");
	puts("--------------<CODE>-------------");
	for(int i=1;;i++){
		printf("%3d. | ",i);
		getline(cin,s[++cnt]);
		if(s[cnt]=="end"){
			cnt--;
			break;
		}
	}
	puts("--------------<INPUT>-------------");
	for(int i=1;i<=cnt;i++){
		if(s[i].substr(0,3)=="lab")lab[s[i].substr(4)]=i;
		if(s[i].substr(0,4)=="scan")cin>>nu[++pos];
	}
	puts("--------------<OUTPUT>--------------");
	run(1);
	puts("\n--------------<END>--------------");
	return system("pause");
}

标签:opt,Opt,Lut,语言,maps,exps,int,name
From: https://www.cnblogs.com/2021changqing52/p/17573579.html

相关文章

  • 【Lut语言(1)】词法分析器
    先为我的Lut语言写个词法分析器再说吧。#include<bits/stdc++.h>usingnamespacestd;stringword[11]={"","int","char","double","goto","if","print","scan","eq","daf"};s......
  • 用c语言打印日历代码
    1、C语言程序编写日历2、C语言年历显示程序设计3、利用c语言输出某月日历4、C语言编程日历显示C语言程序编写日历1、首先要判断一个年份是闰年还是平年,用一个子程序来做。然后就开始写主程序,首先用scanf得到一个年份。在判断这个年份是平年还是闰年后用printf在CMD中打印......
  • c语言计算整数各位数字之和函数
    1、用C语言写一段,可以计算任意两个输入数的和的程序2、求1到100之和用C语言怎么编程3、c语言编写一个求三个整数和的程序并输出结果。4、用c语言编程如何实现求和的程序代码?用C语言写一段,可以计算任意两个输入数的和的程序1、那么因为阿拉伯数字只有10个所以10进制大......
  • c语言编程三个数的最大值
    1、编写一个c语言程序,输入三个整数,输出它们的最大值2、用C语言求3个数中最大的数?3、c语言编程,求abc三个数的最大值4、如何在C语言编程中求取三个数中的最大值编写一个c语言程序,输入三个整数,输出它们的最大值if(cm)m=c;printf(Maxis%d\n,m);}C语言是一门通用计......
  • c语言的一道关于数组的编程题
    1、c语言的一道关于数组的编程题2、编程题:1:定义含有10个元素的数组,并将数组中的元素按逆序从新存放后输...3、c语言编程题:输入10个数存放在一个数组中,输入一个数存入x中,然后找出...c语言的一道关于数组的编程题intcheckNum(intnums[],intlen,intn);//检查n是否存在......
  • C语言学习笔记(三)函数和递归
    函数和递归库函数strcpy()​ 使用之前要先包含<string.h>​ 拷贝时会将\0一起拷贝(注意:/0是字符串结束的标志,但计算长度时不计入)memset()内存设置​ 使用之前要先包含<string.h>#include<stdio.h>#include<string.h>intmain(){ chararr[]="helloworld"; memset(ar......
  • C语言学习笔记(四)数组
    数组一维数组的创建和初始化//创建类型数组名[元素个数];intarr[10];chararr2[5];//初始化intarr[10]={1,2,3};//不完全初始化chararr[5]={'a','b'};chararr3[5]="ab";//每一位为:a,b,末尾有\0chararr6[]="abcdef";//没有确定长度则必须进行初始化,根据初始......
  • C语言编程必备技能!小写转大写
    在C语言中,要区分字母的大小写,可以利用ASCII码中大写字母和小写字母之间的转换关系,差值为32,通过这个差值可以将小写字母转换为大写字母。下面编写程序实现该功能:从键盘输入一个小写字母,按回车键后,程序将该小写字母转换为大写字母,并输出其ASCII值。 程序的算法思想如下:......
  • Flutter错误
    Flutter错误[Flutter错误|不器小窝](https://xingcxb.com/language/dart/b4845b/#%E5%9C%A8flutter%E5%BC%80%E5%8F%91%E4%B8%AD-%E5%B7%B2%E7%BB%8F%E5%AE%89%E8%A3%85%E4%BA%86cocopods-%E4%BD%86%E6%98%AF%E6%8F%90%E7%A4%BA%E6%9C%AA%E5%AE%89%E8%A3%85%E7%9A%84%E9%97%AE%......
  • 7.21语言结构学习
    语言结构学习第一题,答案;第二题,答案写,第一题,答案多少;第二题,答案多少......