📜 ⬆️ ⬇️

We write the simplest scripting programming language in C # (Part 1)

Good afternoon,% username%. Today we will write a scripting programming language in C #, or rather its interpreter.

Prologue


I have been studying C # for a year now, but I can’t learn it normally. Thinking a little bit, I realized that I needed to write something more complicated, and in the process to get experience. I decided to write a Brainfuck interpreter. I wrote it in 5 minutes, but it remained without cycles. Then I started to invent my own language, wrote a bunch of syntax concepts, etc.
Rather, under the cat!


Version v1.0

The first version was not something that was not successful, but simply a failure. Not very convenient syntax and BASIC-like was not a very good tone of programming (in my opinion). The syntax was:
#    VISIBLE , ! IF 5 < 10 VISIBLE 5    10 

')
Version v2.0 - Present

Today it will be about this version of the language. At the moment, this is the most acceptable option. The functionality of this version is inferior to version 1.0, but it is much more convenient.

We write? We write!


Well, finally coding. First we examine the syntax of the language, and its functional model.
Type of function:
 NULL -- writeline(string); write(string); readline[type](variable); F -- ->read(string); ->show(); ->delete(string); ->create(string); 


You probably noticed the strange F and NULL, now more about them.
F and NULL are function types, F is file system operation, NULL does not have a parent.
those. The syntax is:

 #  F,     -> f->create(C:\15.txt); #  ,    writeline(%c !); 


Now it is clear? I think yes.
What is% c? This is equivalent to things like \ n, \ a etc. (Sorry, sclerosis, forgot how they are called).

% c - put a comma (see below)
% n - new line

Coding soon? Yes, right now!


And so the coding of which everyone waited a long time.
Now we will write the main function, it reads the file in the lines and transfers lines to the parser.

 using System; using System.IO; namespace lang { class MainClass { public static void Main (string[] args) { try { using (StreamReader sr = new StreamReader(args[0])) //     { string line; //    while ((line = sr.ReadLine()) != null) //    { Parse.PARSE(line); //  } } } catch (Exception ex) //    catch { Console.Write(" : "); Console.WriteLine(ex.Message); } } } } 

I think everything is very clear here, let's go further.

Parser


So we have Main.cs, funct.cs and Parse.cs . These are all our classes.

Main.cs is what we wrote above
Parse.cs - our string parser
funct.cs - methods

We write a parser.
 using System; namespace lang { public class Parse { public Parse () { } public static void PARSE(string code) { if (code.IndexOf ('#') != 0) { if(code.EndsWith(";")) { int a = code.IndexOf('('); int b = code.IndexOf(')'); string func = code.Substring(0,a); string args = code.Substring(a+1,ba-1); string[] arg = args.Split(','); func = func.Replace(" ", ""); switch(func.ToLower()) { 


At this run is not worth it, we analyze in rows.
public static void PARSE(string code) - This is our method for parsing strings
if (code.IndexOf ('#') != 0) { - If the code does not begin with '#' (This is our comment), then parsim.
if(code.EndsWith(";")) { - Continue to parse if the string ends in a ';'
Further a little more, because we need, at least a little, but the logic.

The model for trimming a string to a function and arguments:

  function(arg1,arg2); ^ ^ ab 


We cut out like this:

string func = code.Substring(0,a); - we start clipping with 0 character to the first '(', as a result we get function
string args = code.Substring(a+1,ba-1); - we get the arguments started from the first character coming after '(' to ");" (+1)
string[] arg = args.Split(','); - create an array with arguments separated by ','
func = func.Replace(" ", ""); - do this in order to avoid an error with a space.
  switch(func.ToLower()) { case "write": funct.write(arg); break; case "writeline": funct.writeline(arg); break; case "readline": funct.readln(arg[0]); break; } 

Here are 3 basic functions, write, writeline and readline.

Functions


We have 3 variables in the language,

  public static string a; public static char b; public static int c; 


You can work with them, this is an innovation of the language in comparison with the previous version.

Feature Code:

 using System; namespace lang { public class funct { public static string a; public static char b; public static int c; public static void writeline (string[] args) { int z = 0; string s = ""; if(args[z] == "a") { // writeline(a);    a Console.WriteLine(a); } else if(args[z] == "b") { // writeline(b);    b Console.WriteLine(b); } else if(args[z] == "c") { // writeline(c);    c Console.WriteLine(c); } else { while(z < args.Length) { //         s += args[z] + " "; z++; } s = s.Replace("%n", Environment.NewLine); // %n    s = s.Replace("%c", ","); // %c   Console.WriteLine(s); //  } } public static void write (string[] args) // writeline { int z = 0; string s = ""; if(args[z] == "a") { Console.WriteLine(a); } else if(args[z] == "b") { Console.WriteLine(b); } else if(args[z] == "c") { Console.WriteLine(c); } else { while(z < args.Length) { s += args[z] + " "; z++; } s = s.Replace("%n", Environment.NewLine); s = s.Replace("%c", ","); Console.Write(s); } } public static void readln (string args) { if(args == "a") { //  == a,    a a = Console.ReadLine(); } else if(args == "b") { //, ,    Console.WriteLine("You can't use \"b\" variable as @STRING."); } else if(args == "c") { //  Console.WriteLine("You can't use \"c\" variable as @STRING."); } } } } 

On it the first part comes to an end, the second part will be about file system. See you!

Source: https://habr.com/ru/post/179529/


All Articles