lang:自制編程語言8——多條解釋

前言

寒假畫了很多時間,終於解釋完了一句話,接下來的任務是對多條語句進行解釋!然後是語義分析,做分支,做for循環,做while循環,做do-until循環,然後是function函數和array特殊數組……先休息一段時間,積累一些知識再做!


MulParser

之前基本把單條語句做完了,只要用一個容器把許多單條語法分析的實例保存好,再分別操作就行了!

#pragma once
#ifndef _MULPARSER_H_
#define _MULPARSER_H_
#include"Parser.h"


namespace sua {
	//多條語句的解釋
	class MulParser
	{
	private:
		std::vector<Parser*>    v_exprs;

	public:
		MulParser();
		~MulParser();


		void create_ast();
		void print_ast();
		void interpret();

	};

};

#endif
#include "MulParser.h"

namespace sua {

	MulParser::MulParser()
	{
		v_exprs.clear();
	}


	MulParser::~MulParser()
	{

		//釋放多行語句
		std::vector<Parser*>().swap(v_exprs);
	}



	void MulParser::create_ast() {
		
		int start = 0,end=0;
		//創建Parser
		for (std::vector<SuatinToken*>::iterator it = global_infix.begin();it!=global_infix.end(); ++it) {
			if ((*it)->type == SuatinTokenType_Sem) {
				Parser* p = new Parser(start, end);
				v_exprs.push_back(p);

				start = end + 1;
			}
			++end;
		}
		//根據Parser構造語法樹
		for (std::vector<Parser*>::iterator it = v_exprs.begin(); it != v_exprs.end(); ++it) {
			(*it)->CreateASTree();
		}


	}

	void MulParser::print_ast() {
		
		//打印語法樹
		for (std::vector<Parser*>::iterator it = v_exprs.begin(); it != v_exprs.end(); ++it) {
			(*it)->ShowASTree();
		}
	}
	void MulParser::interpret() {

		//解釋語法樹
		for (std::vector<Parser*>::iterator it = v_exprs.begin();it!=v_exprs.end(); ++it) {
			(*it)->interpret();
		}

	}

};

項目演示

//test.suatin
a = b = not "yes" and FALSE or 1~="";
c =  1 + (2+3)*4^2^(2+1);
d = e = f = "HELLO b" + "oy!";
a = 123;
1 + 3*4;
詞法分析 time consumed 740 ms
初始化語言環境 time consumed 1 ms
語法分析·創建語法樹 time consumed 10 ms
=
├── a
└── =
    ├── b
    └── not
        └── or
            ├── and
            │   ├── "yes"
            │   └── FALSE
            └── ~=
                ├── 1
                └── ""
=
├── c
└── +
    ├── 1
    └── *
        ├── ()
        │   └── +
        │       ├── 2
        │       └── 3
        └── ^
            ├── 4
            └── ^
                ├── 2
                └── ()
                    └── +
                        ├── 2
                        └── 1
=
├── d
└── =
    ├── e
    └── =
        ├── f
        └── +
            ├── "HELLO b"
            └── "oy!"
=
├── a
└── 123
+
├── 1
└── *
    ├── 3
    └── 4
語法分析·打印語法樹 time consumed 146 ms
[result]false
[result]327681
[result]HELLO boy!
[result]123
[result]13
語法分析·解釋語法樹 time consumed 31 ms
語言環境>
                name   isconst    type      funcPtr     flag             num   str
                 NIL    true       nil     00000000    false               0
              TEST_C    true       int     00000000     true               7
               FALSE    true      bool     00000000    false               0
              TEST_D    true      bool     00000000    false               0
                TRUE    true      bool     00000000     true               0
                   b   false      bool     00000000    false               0
              TEST_B    true    number     00000000     true     1.22323e+06
                   a   false    number     00000000     true             123
                   c   false    number     00000000     true          327681
              TEST_A    true    string     00000000     true               0   author@Demllie
                   d   false    string     00000000     true               0   HELLO boy!
                   e   false    string     00000000     true               0   HELLO boy!
                   f   false    string     00000000     true               0   HELLO boy!
顯示環境信息 time consumed 687 ms
釋放環境 time consumed 0 ms

[CSDN代碼下載地址]
https://download.csdn.net/download/weixin_41374099/12234770

項目代碼下載地址BDWP
鏈接:https://pan.baidu.com/s/1mfYY4T6MHVexwpGVnqJboQ
提取碼:hbfo

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章