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

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)

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);

}

while ((cur < end) && char.IsDigit(m_a[cur]))

cur++;

res = new String(m_a, beg, cur - beg);

} else if (char.IsDigit(m_a[cur])) {

while ((cur < end) && char.IsDigit(m_a[cur]))

cur++;

res = new String(m_a, beg, cur - beg);

} else {

cur--;

res = new String(m_a, beg, cur - beg);

}

} else if (m_a[cur] == '.') {

cur++;

if ((cur == end) || (!char.IsDigit(m_a[cur]))) {

cur--;

res = new String(m_a, beg, cur - beg);

} else {

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);

}

while ((cur < end) && char.IsDigit(m_a[cur]))

cur++;

res = new String(m_a, beg, cur - beg);

} else if (char.IsDigit(m_a[cur])) {

while ((cur < end) && char.IsDigit(m_a[cur]))

cur++;

res = new String(m_a, beg, cur - beg);

} else {

cur--;

res = new String(m_a, beg, cur - beg);

}

} else

res = new String(m_a, beg, cur - beg);

}

} else

res = new String(m_a, beg, cur - beg);

}

if (flag) {

if (IsLD(m_a[cur])) {

while ((cur < end) && IsLD(m_a[cur]))

cur++;

res = new String(m_a, beg, cur - beg);

} else if (m_a[cur] == '\"') {

do {

cur++;

if (m_a[cur] == '\"') {

if ((cur < end - 1) && (m_a[cur + 1] == '\"'))

cur++;

else

break;

}

} while (true);

cur++;

res = new String(m_a, beg, cur - beg);

} else if (cur < end - 1) {

switch (m_a[cur]) {

case ':':

{

cur++;

if (m_a[cur] == '=') {

cur++;

res = ":=";

} else

res = ":";

break;

}

case '~':

{

cur++;

if (m_a[cur] == '=') {

cur++;

res = "~=";

} else

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



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