Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions A#/app/Mark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;

namespace ASharp
{
public class Mark
{
public static Dictionary <string, Mark> marks = new Dictionary<string, Mark>();
private string meaning;
public string M
{
get
{
return meaning;
}
set
{
if (value != null)
{
this.meaning = value;
}
}
}
public Mark(string markName)
{
M = markName;
}
public static void Creating(string name, string meaning) // создать метку
{
marks[name] = new Mark(meaning);
}
public static int Search(string str) // найти метку
{
foreach (KeyValuePair<string, Mark> item in marks)
{
if (str == item.Key)
{
return Convert.ToInt32(item.Value.meaning);
}
}
return -1; // метки не найдено, развалить программу
}
public static int Move(string go) // перейти по метке
{
return Mark.Search(go.Trim(':'));
}
}
}
174 changes: 174 additions & 0 deletions A#/app/Mathem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
using System;
using System.Collections.Generic;

namespace ASharp
{
public class Mathem
{
public static string Result(string n1, string n2, string operation)
{
switch (operation)
{
case "+":
{
return Sum(n1, n2);
}
case "-":
{
return Min(n1, n2);
}
case "*":
{
return Multiply(n1, n2);
}
case "/":
{
return Divide(n1, n2);
}
case "%":
{
return Mod(n1, n2);
}
case "^":
{
return Power(n1, n2);
}
default:
{
return "bad";
}
}
}
public static int InDigit(string n1) // определить число или переменная
{
if (Int32.TryParse(n1, out int num1) == true)
{
return Convert.ToInt32(n1);
}
else
{
return Variables.Search(n1);
}
}
public static bool More(string n1, string n2)
{
int num1 = Mathem.InDigit(n1);
int num2 = Mathem.InDigit(n2);

if (num1 > num2)
{
return true;
}
else
{
return false;
}
}
public static bool Less(string n1, string n2)
{
int num1 = Mathem.InDigit(n1);
int num2 = Mathem.InDigit(n2);

if (num1 < num2)
{
return true;
}
else
{
return false;
}
}
public static bool Equally(string n1, string n2)
{
int num1 = Mathem.InDigit(n1);
int num2 = Mathem.InDigit(n2);

if (num1 == num2)
{
return true;
}
else
{
return false;
}
}
public static bool MoreOrEqually(string n1, string n2)
{
int num1 = Mathem.InDigit(n1);
int num2 = Mathem.InDigit(n2);

if (num1 >= num2)
{
return true;
}
else
{
return false;
}
}
public static bool LessOrEqually(string n1, string n2)
{
int num1 = Mathem.InDigit(n1);
int num2 = Mathem.InDigit(n2);

if (num1 <= num2)
{
return true;
}
else
{
return false;
}
}
public static bool NotEqually(string n1, string n2)
{
int num1 = Mathem.InDigit(n1);
int num2 = Mathem.InDigit(n2);

if (num1 != num2)
{
return true;
}
else
{
return false;
}
}
public static string Sum(string n1, string n2)
{
int num1 = Mathem.InDigit(n1);
int num2 = Mathem.InDigit(n2);
return Convert.ToString(num1 + num2);
}
public static string Min(string n1, string n2)
{
int num1 = Mathem.InDigit(n1);
int num2 = Mathem.InDigit(n2);
return Convert.ToString(num1 - num2);
}
public static string Multiply(string n1, string n2)
{
int num1 = Mathem.InDigit(n1);
int num2 = Mathem.InDigit(n2);
return Convert.ToString(num1 * num2);
}
public static string Divide(string n1, string n2)
{
int num1 = Mathem.InDigit(n1);
int num2 = Mathem.InDigit(n2);
return Convert.ToString(num1 / num2);
}
public static string Mod(string n1, string n2)
{
int num1 = Mathem.InDigit(n1);
int num2 = Mathem.InDigit(n2);
return Convert.ToString(num1 % num2);
}
public static string Power(string n1, string n2)
{
int num1 = Mathem.InDigit(n1);
int num2 = Mathem.InDigit(n2);
return Convert.ToString(Math.Pow(num1, num2));
}
}
}
124 changes: 124 additions & 0 deletions A#/app/Parser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;


namespace ASharp
{
public static class Parser
{
public static void Parsing(string[] textOfProgram)
{
// найти и запомнить метки
for (int i = 0; i < textOfProgram.Length; i++)
{
if ( textOfProgram[i].EndsWith(":") && textOfProgram[i+1] != "=" && Mark.Search(textOfProgram[i]) == -1)
{
Mark.Creating(textOfProgram[i].Trim(':'), Convert.ToString(i));
}
}

// основная работа
for (int i = 0; i < textOfProgram.Length - 6; i++)
{
// условные конструкции
if (textOfProgram[i] == "if" && textOfProgram[i+4] == "goto")
{
bool b = false;
switch (textOfProgram[i+2])
{
case ">":
b = Mathem.More(textOfProgram[i+1],textOfProgram[i+3]);
break;
case "<":
b = Mathem.Less(textOfProgram[i+1],textOfProgram[i+3]);
break;
case "==":
b = Mathem.Equally(textOfProgram[i+1],textOfProgram[i+3]);
break;
case ">=":
b = Mathem.MoreOrEqually(textOfProgram[i+1],textOfProgram[i+3]);
break;
case "<=":
b = Mathem.LessOrEqually(textOfProgram[i+1],textOfProgram[i+3]);
break;
case "!=":
b = Mathem.NotEqually(textOfProgram[i+1],textOfProgram[i+3]);
break;
default:
b = false;
break;
}

if (b == true)
{
i = Mark.Move(textOfProgram[i+5]);
}
else
{
i = i + 6;
}
}

// переход по меткам
if (textOfProgram[i] == "goto")
{
i = Mark.Move(textOfProgram[i+1]);
}

// операции с переменными
if (Int32.TryParse(textOfProgram[i], out int x1 ) == false && textOfProgram[i+1] == "=")
{
string forVariable = textOfProgram[i+2];

if (Int32.TryParse( textOfProgram[i+2], out int x2) == false)
{
forVariable = Convert.ToString(Variables.Search(textOfProgram[i+2]));
}

if (Parser.WordInList(textOfProgram[i+3], new List<string> {"+", "-", "*", "/", "%", "^"}) == true)
{
forVariable = Mathem.Result(textOfProgram[i+2], textOfProgram[i+4], textOfProgram[i+3]);
}

if (Parser.WordInList(textOfProgram[i], new List<string> {"=", "+", "-", "*", "/", "%", "^", "print", "read", "goto", "if", "==", "!=", ">", "<", ">=", "<=", "testName"}) == false)
{
Variables.Creating(textOfProgram[i], forVariable);
}
}

// напечатать переменную
if (textOfProgram[i] == "print")
{
System.Console.WriteLine(Variables.Print(textOfProgram[i+1]));
}

// считать переменную
if (textOfProgram[i] == "read")
{
Console.Write(textOfProgram[i+1] + " = ");
string forVariable = Console.ReadLine();
if (Int32.TryParse(forVariable, out int x3) == true)
{
Variables.Creating(textOfProgram[i+1], forVariable);
}
else
{
throw new Exception(" недопустимое значение переменной ");
}
}
}
}

static public bool WordInList(string str, List<string> list) // если подстрока есть в листе - true, иначе false
{
foreach (string i in list)
{
if (i == str)
{
return true;
}
}
return false;
}
}
}
Loading