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

return this.Clone() as VarBase;

}

public abstract System.Object Clone();

public override abstract string ToString();

public abstract void Serialise(BinaryWriter bw);

}

}

2. Класс ArrayVar.

using System.Collections;

using System.IO;

namespace interpr.logic.vartypes {

public class ArrayVar : VarBase {

public virtual IntVar Size {

get { return new IntVar(m_list.Count); }

}

private ArrayList m_list;

public ArrayVar() {

m_list = new ArrayList();

}

public int GetSize() {

return m_list.Count;

}

public void setAt(int index, SingleVar var) {

if (var == null) {

throw new CalcException("Ошибка");

}

if (index < 0)

throw new CalcException("Индекс не может быть отрицательным");

for (int ind = index, s = m_list.Count; ind >= s; ind--)

m_list.Add(null);

m_list[index] = var.Clone();

}

public SingleVar getAt(int index) {

if (index < 0)

throw new CalcException("Индекс не может быть отрицательным");

if (index >= m_list.Count)

throw new CalcException("Выход за пределы массива");

else

return (SingleVar) m_list[index];

}

public SingleVar this[int index] {

get { return getAt(index); }

set { setAt(index, value); }

}

public IntVar IsElementDefined(int index) {

bool result = index>=0;

result = result&&(index<m_list.Count);

result = result&&(m_list[index]!=null);

return new IntVar(result);

}

public override System.Object Clone() {

ArrayVar res = new ArrayVar();

int li = 0;

SingleVar e = null;

while (li < m_list.Count) {

e = (SingleVar) m_list[li++];

if (e != null)

res.m_list.Add(e.Clone());

else

res.m_list.Add(null);

}

return res;

}

public override void Serialise(BinaryWriter bw) {

bw.Write('a');

int size = m_list.Count;

bw.Write(size);

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

if (m_list[i] == null)

bw.Write('n');

else

(m_list[i] as VarBase).Serialise(bw);

}

}

public override System.String ToString() {

System.String res = "[";

int li = 0;

SingleVar e = null;

if (li < m_list.Count) {

e = (SingleVar) m_list[li++];

if (e != null) {

res += e.ToString();

}

else

res += "-";

}

while (li < m_list.Count) {

e = (SingleVar) m_list[li++];

if (e != null) {

res += ", " + e.ToString();

}

else

res += ", -";

}

return res + "]";

}

}

}

3. Класс InterprEnvironment.

using System;

using System.Collections;

using System.IO;

namespace interpr.logic {

public class InterprEnvironment {

private SubroutinesManager m_subsman = null;

private ConsoleNamespace m_console_vars;

private bool m_not_restored = false;

public bool NotRestored {

get { return m_not_restored; }

}

public ConsoleNamespace ConsoleNamespace {

get { return m_console_vars; }

}

public ConsoleNamespace.VariableReport[] GetGlobalVarsList() {

return m_console_vars.GetVariableList();

}

private InterprEnvironment() {

m_current_namespace = new ConsoleNamespace();

m_console_vars = m_current_namespace as ConsoleNamespace;

m_not_restored = false;

try {

m_console_vars.Restore();

} catch {

m_not_restored = true;

m_console_vars = new ConsoleNamespace();

m_current_namespace = m_console_vars;

}

}

public void LoadSubs() {

if (m_current_console == null)

throw new OtherException("Error in Environment.LoadSubs()");

s_instance.m_subsman = SubroutinesManager.GetInstance();

s_instance.m_subsman.ReloadAll();

}

private static InterprEnvironment s_instance = null;

public static InterprEnvironment Instance {

get {

if (s_instance == null)

s_instance = new InterprEnvironment();

return s_instance;

}

}

public static void Reset() {

s_instance = new InterprEnvironment();

}

public void SaveVars() {

m_console_vars.Save();

}

public bool LoadSub(string name) {

return m_subsman.Load(name);

}

private Namespace m_current_namespace = null;

public Namespace CurrentNamespace {

get { return m_current_namespace; }

set { m_current_namespace = value; }

}

private IConsole m_current_console = null;

public IConsole CurrentConsole {

get { return m_current_console; }

set { m_current_console = value; }

}

public Operation GetFunction(string name) {

if (name == "abs")

return Operation.ABS;

...........................

if (name == "size")

return Operation.SIZE;

return new SubName(name);

}

public string[] LoadedSubs {

get { return m_subsman.SubroutineNames; }

}

private class SubroutinesManager {

private ArrayList m_subs = new ArrayList();

private ArrayList m_names = new ArrayList();

private SubroutinesManager() {

DirectoryInfo di =

new DirectoryInfo(Directory.GetCurrentDirectory() + @"\subroutines");

if (!di.Exists) {

di.Create();

}

}

public bool Load(string name) {

FileInfo fi = new FileInfo(Directory.GetCurrentDirectory() + @"\subroutines\" + name);

if (!fi.Exists)

throw new OtherException("Error in SubroutinesManager.Load()");

return LoadFile(fi);

}

private bool LoadFile(FileInfo file) {

try {

StreamReader sr = file.OpenText();

LinkedList ll = new LinkedList();

try {

while (sr.Peek() != -1) {

ll.AddFirst(sr.ReadLine());

}

} finally {

sr.Close();

}

string[] strs = new String[ll.Count];

int i = 0;

while (!ll.IsEmpty()) {

strs[i] = (ll.RemoveLast() as String);

i++;

}

Subroutine sub;

try {

sub = new Subroutine(strs, file.Name);

} catch (LineSyntaxException ex) {

InterprEnvironment.Instance.CurrentConsole.PrintLn("Синтаксическая ошибка в " + ex.Function + "[] at line " + ex.Line + " " + ex.Message);

return false;

} catch (SyntaxErrorException ex) {

InterprEnvironment.Instance.CurrentConsole.PrintLn("Синтаксическая ошибка в " + file.Name + " " + ex.Message);

return false;

}

Set(file.Name, sub);

} catch {

throw new OtherException("Error in Environment.Load()");

}

return true;

}

public Subroutine this[string name] {

get {

int sres = m_names.IndexOf(name);

if (sres < 0)

return null;

else

return m_subs[sres] as Subroutine;

}

}

private void Set(string name, Subroutine sub) {

int sres = m_names.IndexOf(name);

if (sres >= 0) {

m_names.RemoveAt(sres);

m_subs.RemoveAt(sres);

}

m_names.Add(name);

m_subs.Add(sub);

}

private static SubroutinesManager s_inst = null;

public static SubroutinesManager GetInstance() {

if (s_inst == null)

s_inst = new SubroutinesManager();

return s_inst;

}

public string[] SubroutineNames {

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



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