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
8 changes: 8 additions & 0 deletions ASharp/ASharp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

</Project>
12 changes: 12 additions & 0 deletions ASharp/ActionMark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace ASharp
{
class ActionMark
{
public static void Mark(string mark)
{
int line = Program.Marks[mark];
}
}
}
18 changes: 18 additions & 0 deletions ASharp/ActionPrint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.IO;
using System.Collections.Generic;

namespace ASharp
{
class ActionPrint
{
public static void Print(string action)
{
if(Program.Variables.ContainsKey(action))
{
Console.WriteLine($"{action} = {Program.Variables[action]}");
}
else Console.WriteLine($"Переменной {action} не существует");
}
}
}
17 changes: 17 additions & 0 deletions ASharp/ActionRead.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ASharp
{
class ActionRead
{
public static void Read(string action)
{
Console.Write($"{action} = ");
int number = Convert.ToInt32(Console.ReadLine());
Program.SetVariable(action, number);
}
}
}
47 changes: 47 additions & 0 deletions ASharp/CodeParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Text.RegularExpressions;

namespace ASharp
{
class CodeParser
{
public void Parse(string[] code)
{
int stringCounter = -1;

for(int i = 0; i <= code.Length - 1; i++)
{
stringCounter++;

string[] splitedString = code[i].Split(' ');

switch (splitedString[0])
{
case "read":
ActionRead.Read(splitedString[1]);
break;
case "print":
ActionPrint.Print(splitedString[1]);
break;
default:
break;
}

if(code[i].Contains("+") || code[i].Contains("-") || code[i].Contains("*") || code[i].Contains("/") || code[i].Contains("="))
{
Math.MathParser(code[i]);
}
else if(code[i].Contains("if"))
{
i = Conditions.ParseConditions(code[i], stringCounter);
stringCounter = i;
}
else if(code[i].Contains(":"))
{
string[] mark = code[i].Split(':');
Program.SetMark(mark[0], stringCounter);
}
}
}
}
}
33 changes: 33 additions & 0 deletions ASharp/CodeParser_Default
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Text.RegularExpressions;

namespace ASharp
{
class CodeParser
{
public string Parse(string[] code)
{
string pattern = @"([a-z]+)\s(\w+)";
foreach (string line in code)
{
foreach(Match i in Regex.Matches(line, pattern))
{
switch (i.Groups[1].Value)
{
case "read":
ActionRead.Read(i.Groups[2].Value);
break;
case "print":
ActionPrint.Print(i.Groups[2].Value);
break;
default:
break;
}
}

}

return "";
}
}
}
18 changes: 18 additions & 0 deletions ASharp/CodeReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.IO;

namespace ASharp
{
class CodeReader
{
public static string[] ReadFile(string path)
{
if (Path.HasExtension(path))
{
string[] code = File.ReadAllLines(path);
return code;
}

return null;
}
}
}
74 changes: 74 additions & 0 deletions ASharp/Conditions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Text.RegularExpressions;

namespace ASharp
{
class Conditions
{
public static int ParseConditions(string code, int counter)
{
string pattern = @"([if]+)\s([\w]+)\s([\><!=]+)\s([\w]+)\s([goto]+)\s([\w]+)";
Match i = Regex.Match(code, pattern);

switch (i.Groups[3].Value)
{
case ">":
if(Math.Converter(i.Groups[2].Value) > Math.Converter(i.Groups[4].Value))
{
return Program.Marks[i.Groups[6].Value] - 1;
}
else
{
return counter;
}
case ">=":
if(Math.Converter(i.Groups[2].Value) >= Math.Converter(i.Groups[4].Value))
{
return Program.Marks[i.Groups[6].Value] - 1;
}
else
{
return counter;
}
case "<":
if(Math.Converter(i.Groups[2].Value) < Math.Converter(i.Groups[4].Value))
{
return Program.Marks[i.Groups[6].Value] - 1;
}
else
{
return counter;
}
case "<=":
if(Math.Converter(i.Groups[2].Value) <= Math.Converter(i.Groups[4].Value))
{
return Program.Marks[i.Groups[6].Value] - 1;
}
else
{
return counter;
}
case "==":
if(Math.Converter(i.Groups[2].Value) == Math.Converter(i.Groups[4].Value))
{
return Program.Marks[i.Groups[6].Value] - 1;
}
else
{
return counter;
}
case "!=":
if(Math.Converter(i.Groups[2].Value) != Math.Converter(i.Groups[4].Value))
{
return Program.Marks[i.Groups[6].Value] - 1;
}
else
{
return counter;
}
default:
return 0;
}
}
}
}
88 changes: 88 additions & 0 deletions ASharp/Math.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Text.RegularExpressions;

namespace ASharp
{
class Math
{
public static int Converter(string line)
{
int res;
if(Program.Variables.ContainsKey(line))
{
return Program.Variables[line];
}
else if(Int32.TryParse(line, out res))
{
return res;
}
else
{
Console.WriteLine($"Переменной {line} не сушествует");
return 0;
}
}

public static void MathParser(string code)
{
string pattern = @"(\w+)\s*(=)\s*(\w+)\s*([\/\+\-\*]+)\s*(\w+)";

Match i = Regex.Match(code, pattern);

switch (i.Groups[4].Value)
{
case "+":
if (Program.Variables.ContainsKey(i.Groups[1].Value))
{
Program.Variables[i.Groups[1].Value] = Converter(i.Groups[3].Value) + Converter(i.Groups[5].Value);
}
else
{
Program.Variables.Add(i.Groups[1].Value, Converter(i.Groups[3].Value) + Converter(i.Groups[5].Value));
}
break;
case "-":
if (Program.Variables.ContainsKey(i.Groups[1].Value))
{
Program.Variables[i.Groups[1].Value] = Converter(i.Groups[3].Value) - Converter(i.Groups[5].Value);
}
else
{
Program.Variables.Add(i.Groups[1].Value, Converter(i.Groups[3].Value) - Converter(i.Groups[5].Value));
}
break;
case "*":
if (Program.Variables.ContainsKey(i.Groups[1].Value))
{
Program.Variables[i.Groups[1].Value] = Converter(i.Groups[3].Value) * Converter(i.Groups[5].Value);
}
else
{
Program.Variables.Add(i.Groups[1].Value, Converter(i.Groups[3].Value) * Converter(i.Groups[5].Value));
}
break;
case "/":
if (Program.Variables.ContainsKey(i.Groups[1].Value))
{
Program.Variables[i.Groups[1].Value] = Converter(i.Groups[3].Value) / Converter(i.Groups[5].Value);
}
else
{
Program.Variables.Add(i.Groups[1].Value, Converter(i.Groups[3].Value) / Converter(i.Groups[5].Value));
}
break;
default:
string[] splitedString = code.Split(' ');
if (Program.Variables.ContainsKey(splitedString[0]))
{
Program.Variables[splitedString[0]] = Converter(splitedString[2]);
}
else
{
Program.Variables.Add(splitedString[0], Converter(splitedString[2]));
}
break;
}
}
}
}
38 changes: 38 additions & 0 deletions ASharp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;

namespace ASharp
{
class Program
{
public static Dictionary<string, int> Variables = new Dictionary<string, int>();
public static Dictionary<string, int> Marks = new Dictionary<string, int>();

public static void SetVariable(string key, int value)
{
if(!Variables.ContainsKey(key))
{
Variables.Add(key, value);
}
}

public static void SetMark(string key, int value)
{
if(!Marks.ContainsKey(key))
{
Marks.Add(key, value);
}
}

static void Main(string[] args)
{
string path = "./code.txt";

string[] code = CodeReader.ReadFile(path);
CodeParser parser = new CodeParser();
parser.Parse(code);

Console.ReadKey();
}
}
}
7 changes: 7 additions & 0 deletions ASharp/code.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
read N
R = 1
step:
R = R * N
N = N - 1
if N > 1 goto step
print R
1 change: 1 addition & 0 deletions ASharp/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"if goto" worked only like repeat
Loading