Êóðñîâàÿ ðàáîòà: Èíòåðàêòèâíûé èíòåðïðåòàòîð
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) Operation.NOT;
private static int Priority(Operation op)
return 1;
private static bool IsBinary(Operation op) {
return Priority(op) < 6;
}
private static bool IsUnary(object obj)
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 {
public override int ReqCount {
get { return 1; }
}
public override VarBase Perform(ArgList al) {
if (al.Count != ReqCount)
throw new OtherException("Invalid argument list");
al.Reset();
VarBase arg1 = al.Get();
if (arg1 is IntVar)
return ((arg1 as IntVar).Val>0) ? (arg1.Clone() as IntVar) : (new IntVar(-((IntVar) arg1).Val));
else if (arg1 is RealVar)
return ((arg1 as RealVar).Val>0) ? (arg1.Clone() as RealVar) : (new RealVar(-((RealVar) arg1).Val));
else
throw new CalcException("Íåïðàâèëüíûå àðãóìåíòû ôóíêöèè");
}
}
public static readonly Operation ADD = new ADD_c();
private class ADD_c : Operation {
public override int ReqCount {
get { return 2; }
}
public override VarBase Perform(ArgList al) {
if (al.Count != ReqCount)
throw new OtherException("Invalid argument list");
al.Reset();
VarBase arg1 = al.Get();
VarBase arg2 = al.Get();
if(!(arg1.IsSingle()&&arg2.IsSingle()))
throw new CalcException("Íåâåðíûå òèïû îïåðàíäîâ");
return (arg1 as SingleVar).add(arg2 as SingleVar);
}
}
public static readonly Operation AND = new AND_c();
private class AND_c : Operation {
public override int ReqCount {
get { return 2; }
}
public override VarBase Perform(ArgList al) {
if (al.Count != ReqCount)
throw new OtherException("Invalid argument list");
al.Reset();
VarBase arg1 = al.Get();
VarBase arg2 = al.Get();
if(!(arg1.IsSingle()&&arg2.IsSingle()))
throw new CalcException("Íåâåðíûå òèïû îïåðàíäîâ");
return (arg1 as SingleVar).and(arg2 as SingleVar);
}
}
.......................................................................................
}
}
10. Êëàññ Parser.
using System;
using System.Collections;
namespace interpr.logic {
public class Parser : IEnumerable, IEnumerator {
private char[] m_a;
private int m_len;
private int m_cur = 0;
private int m_new_cur = -1;
private bool m_at_begin;
private static readonly string[] s_keywords =
new string[] {
"if",
"else",
"elseif",
"endif",
"while",
"loop",
"return",
"call",
"print",
"println",
"readln",
"clear",
"for",
"next",
"error"
};
private static readonly int s_keywords_length = s_keywords.Length;
private static bool IsLD(char c) (c ==
private static bool IsSp(char c)
public static bool IsID(string str)
public void Reset() {
m_cur = 0;
m_new_cur = -1;
m_at_begin = true;
}
public string GetString() {
return new String(m_a, 0, m_len);
}
public bool HasMore() {
return m_cur < m_len;
}
public Parser(string str) {
char[] a = str.ToCharArray();
int n = a.Length;
int i = 0;
int j = 0;
m_a = new char[n];
while (i < n) {
if (a[i] == '#') {
break;
} else if (a[i] == '\"') {
m_a[j] = '\"';
i++;
j++;
while ((i < n) && (a[i] != '\"')) {
m_a[j] = a[i];
i++;
j++;
}
if (i == n)
throw new SyntaxErrorException("Íå çàêðûòàÿ ñòðîêîâàÿ êîíñòàíòà");
else {
m_a[j] = '\"';
i++;
j++;
}
} else if (IsSp(a[i])) {
bool flag = false;
if ((i > 0) && (IsLD(a[i - 1]))) {
m_a[j] = ' ';
j++;
flag = true;
}
while ((i < n) && IsSp(a[i]))
i++;
if (((i == n) || (!IsLD(a[i]))) && flag)
j--;
} else {
m_a[j] = a[i];
i++;
j++;
}
}
m_len = j;
Reset();
}
private string GetCurrent() {
int cur = m_cur;
int beg = m_cur;
int end = m_len;
string res = null;
bool flag = true;
if ((m_a[cur] == '.') && ((cur < end - 1) && (!char.IsDigit(m_a[cur + 1]))) || (cur == end - 1)) {
flag = true;
} else if (char.IsDigit(m_a[cur]) || (m_a[cur] == '.')) {
flag = false;
while ((cur < end) && char.IsDigit(m_a[cur]))
cur++;
if (cur == end) {
res = new String(m_a, beg, cur - beg);
} else if ((m_a[cur] == 'e') || (m_a[cur] == 'E')) {
cur++;
if (cur == end) {
cur--;
res = new String(m_a, beg, cur - beg);
} else if ((m_a[cur] == '+') || (m_a[cur] == '-')) {
cur++;
if ((cur == end) || (!char.IsDigit(m_a[cur]))) {
cur -= 2;
res = new String(m_a, beg, cur - beg);
}
Ñòðàíèöû: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14