Рефераты. Интерактивный интерпретатор

* сначала отдельно разбираются все операнды, затем в результат

* дописывается сама функция, как операция

* ** Обращение к элементу массива обрабатывается аналогично

* В конце все оставшиеся в стеке операции выталкиваются в результат

*/

InterprEnvironment env = InterprEnvironment.Instance;

if (tokens.IsEmpty()) return;

LinkedList.Iterator itr = tokens.GetIterator();

LinkedList stk = new LinkedList();

while (itr.HasMore) {

string si = (itr.Step() as System.String);

if (si == "(") {

stk.Add(O_BR);

}

else if (si == ")") {

while (true) {

object o = stk.RemoveLast();

if (o == O_BR) break;

AddFront(new Call(o as Operation));

}

while ((!stk.IsEmpty()) && IsUnary(stk.Last)) {

AddFront(new Call(stk.RemoveLast() as Operation));

}

}

else if (Parser.IsID(si)) {

bool bfun = false;

bool barray = false;

if (itr.HasMore) {

string s = (itr.Step() as System.String);

if (s == "[")

bfun = true;

else if (s == "{")

barray = true;

else

itr.Previous();

}

if (bfun) {

LinkedList l = null;

while (true) {

l = new LinkedList();

int level = 0;

while (true) {

if (!itr.HasMore)

throw new SyntaxErrorException("Синтаксическая ошибка в выражении");

string sj = (itr.Step() as System.String);

if (sj == "[") {

level++;

l.Add(sj);

}

else if (sj == "]") {

if (level == 0)

goto label1;

else {

level--;

l.Add(sj);

}

}

else if (sj == ",") {

if (level > 0)

l.Add(sj);

else

break;

}

else

l.Add(sj);

}

OPZ(l);

}

label1:

if (l != null)

OPZ(l);

Operation sub = env.GetFunction(si);

AddFront(new Call(sub));

while ((stk.Count > 0) && IsUnary(stk.Last)) {

AddFront(new Call((Operation) stk.RemoveLast()));

}

}

else if (barray) {

LinkedList l = new LinkedList();

int level = 0;

while (true) {

if (!itr.HasMore)

throw new SyntaxErrorException("Синтаксическая ошибка в выражении");

String sj = (String) itr.Step();

if (sj == "{") {

level++;

l.Add(sj);

}

else if (sj == "}") {

if (level == 0)

break;

else {

level--;

l.Add(sj);

}

}

else

l.Add(sj);

}

OPZ(l);

VarName v = new VarName(si);

AddFront(v);

AddFront(new Call(Operation.INDEX));

while ((stk.Count > 0) && IsUnary(stk.Last)) {

AddFront(new Call(stk.RemoveLast() as Operation));

}

}

else {

VarName v = new VarName(si);

AddFront(v);

while ((stk.Count > 0) && IsUnary(stk.Last)) {

AddFront(new Call(stk.RemoveLast() as Operation));

}

}

}

else {

Operation op = StrToOperation(si);

if (op == null) {

SingleVar sv = SingleVar.FromString(si);

if (si == null)

throw new SyntaxErrorException("Синтаксическая ошибка в выражении");

AddFront(sv);

while ((stk.Count > 0) && IsUnary(stk.Last)) {

AddFront(new Call(stk.RemoveLast() as Operation));

}

}

else {

//operation

if (op == Operation.ADD) {

itr.Previous();

if (!itr.HasPrevious) {

stk.Add(Operation.UPLUS);

itr.Step();

continue;

}

String strpr = (String) itr.Previous();

itr.Step();

itr.Step();

if ((StrToOperation(strpr) != null) || (strpr == "(") ||

(strpr == "[") || (strpr == "{")) {

stk.Add(Operation.UPLUS);

continue;

}

}

else if (op == Operation.SUB) {

itr.Previous();

if (!itr.HasPrevious) {

stk.Add(Operation.UMINUS);

itr.Step();

continue;

}

String strpr = (String) itr.Previous();

itr.Step();

itr.Step();

if ((StrToOperation(strpr) != null) || (strpr == "(") ||

(strpr == "[") || (strpr == "{")) {

stk.Add(Operation.UMINUS);

continue;

}

}

else if (op == Operation.NOT) {

stk.Add(op);

continue;

}

if (stk.IsEmpty() || (stk.Last == O_BR)) {

stk.Add(op);

}

else {

int pr = Priority(op);

while (true) {

if (stk.IsEmpty())

break;

Object stktop = stk.Last;

if (stktop is Operation) {

int pr1 = Priority(stktop as Operation);

if ((pr <= pr1) && (pr1 < 6)) {

AddFront(new Call(stktop as Operation));

stk.RemoveLast();

}

else

break;

}

else

break;

}

stk.Add(op);

}

}

}

}

while (!stk.IsEmpty()) {

Object o = stk.RemoveLast();

AddFront(new Call(o as Operation));

}

}

public VarBase Calculate() {

if (m_c == 0)

throw new CalcException("Ошибка: пустое выражение.");

Element top1 = null;

Element cur = m_top;

try {

for (; cur != null; cur = cur.m_next) {

if (cur.m_o is Call) {

int rc = (cur.m_o as Call).ReqCount;

ArgList al = new ArgList();

for (int i = 0; i < rc; i++) {

if (top1 == null)

throw new CalcException("Ошибка при вычислении выражения");

al.Add(top1.m_o.Compute());

top1 = top1.m_next;

}

(cur.m_o as Call).SetArgList(al);

top1 = new Element((cur.m_o as Call).Compute(), top1);

}

else {

top1 = new Element(cur.m_o, top1);

}

}

if ((top1 == null) || (top1.m_next != null))

throw new CalcException("Ошибка при вычислении выражения");

return top1.m_o.Compute();

}

catch (CalcException ex) {

throw ex;

}

catch {

throw new CalcException("Ошибка при вычислении выражения");

}

}

private static Operation StrToOperation(String str)

private static int Priority(Operation op) (op == Operation.GT))

private static bool IsBinary(Operation op) {

return Priority(op) < 6;

}

private static bool IsUnary(object obj)

return ((obj == Operation.NOT)

private class BR_c {}

private static object O_BR = new BR_c();

}

}

9. Класс Operation (сокращенно).

using System;

using interpr.logic.vartypes;

namespace interpr.logic {

public abstract class Operation {

public abstract int ReqCount { get; }

public abstract VarBase Perform(ArgList al);

public static readonly Operation ABS = new ABS_c();

private class ABS_c : Operation {

Страницы: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17



2012 © Все права защищены
При использовании материалов активная ссылка на источник обязательна.