Skip to content

@bencz /main.cs
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Extreme basic hiper basic master hiper mega basic test to include system
/*
Sample:
file: main.in
// Start with defines
@define basic = std'basic
@define type = sys'types
// code
symbol type'int32 main
{
basic'PrintLine()
};
>------------------------------------
File: include\std\basic.in
@define type = sys'types
symbol type'String PrintLine
{
System.Console.WriteLine("Ola mundo")
}
>------------------------------------
File: include\sys\types.in
definition int16 Int16
definition int32 Int32
definition int64 Int64
definition string String
>------------------------------------
File: include\langBase.in
&&symbol [frmt] <name>
&&method [frmt] <name>
&&procedure <name>
&&function [frmt] <name>
&&class [frmt] <name>
&&definition <name> <value>
&&@define <pName> = <incName>
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sample
{
public abstract class SimpleParse
{
public Stream stream;
public StreamReader sr;
public string include_dir = @"..\include\";
private string _fName;
public string FileName
{
get { return _fName; }
set { _fName = value; }
}
private string[] _args;
public string[] args
{
get { return _args; }
set { _args = value; }
}
private string _AllSource;
public string AllSource
{
get { return _AllSource; }
set { _AllSource = value; }
}
private int _line;
public int line
{
get { return _line; }
set { _line = value; }
}
private int _column;
public int column
{
get { return _column; }
set { _column = value; }
}
private int _counter;
public int counter
{
get { return _counter; }
set { _counter = value; }
}
private bool _EOF;
public bool EOF
{
get { return _EOF; }
set { _EOF = value; }
}
public Dictionary<string, int> StopedParse = new Dictionary<string, int>();
public List<string> FileInclude = new List<string>();
public List<bool> IsFIleProcessed = new List<bool>();
// if true use: [frmt] <name>
// if faslse use: <name>
public Dictionary<string, bool> SyntaxFile = new Dictionary<string, bool>();
// Stay with all commands syntax :)
// eg: @define*STRING* <pName>*ARRAY[0]* =*ARRAY[1]* <incName>*ARRAY[2]*
public Dictionary<string, Array> SyntaxFileCommands = new Dictionary<string, Array>();
}
public class Lexical : SimpleParse
{
public Lexical() { }
public string ReadWord()
{
return Read();
}
private string Read()
{
column = 1;
line = 1;
bool IsToken = false;
bool IsString = false;
bool IsNumber = false;
bool IsComment = false;
string token = string.Empty;
string TokenString = string.Empty;
string TokenNumber = string.Empty;
string padr = string.Empty;
for (int i = counter; i < AllSource.Length; i++)
{
if (IsToken)
{
for (int j = i; j < AllSource.Length; j++)
{
if (AllSource[j] == ' ')
{
column++;
IsToken = false;
padr = token;
token = string.Empty;
counter = j;
break;
}
else if (AllSource[j] == '\n' || AllSource[j] == '\r' || AllSource[j] == '\0')
{
column = 0;
IsToken = false;
padr = token;
token = string.Empty;
line++;
counter = j;
break;
}
else
{
column++;
token += AllSource[j];
}
}
}
else if (IsString)
{
for (int j = i; j < AllSource.Length; j++)
{
if (AllSource[j] == '"')
{
column++;
IsString = false;
padr = TokenString;
TokenString = string.Empty;
break;
}
else if (AllSource[j] == '\n' || AllSource[j] == '\r' || AllSource[j] == '\0')
{
Console.WriteLine("Erro...: {0} : {1} ", line, column);
}
else
{
column++;
TokenString += AllSource[j];
}
}
}
else if (IsNumber)
{
for (int j = i; j < AllSource.Length; j++)
{
if (AllSource[j] == ' ')
{
column++;
IsNumber = false;
padr = TokenNumber;
TokenNumber = string.Empty;
break;
}
else if (AllSource[j] == '\n' || AllSource[j] == '\r' || AllSource[j] == '\0')
{
line++;
IsNumber = false;
padr = TokenNumber;
TokenNumber = string.Empty;
break;
}
else
{
if (char.IsNumber(AllSource[j]))
{
column++;
TokenNumber += AllSource[j];
}
else
{
IsNumber = false;
TokenNumber = string.Empty;
break;
}
}
}
}
else if (IsComment)
{
for (int j = i; j < AllSource.Length; j++)
{
if (AllSource[j] == '\n' || AllSource[j] == '\r')
{
counter = j;
IsComment = false;
break;
}
}
i = counter;
}
else
{
if (AllSource[i] == '/' && AllSource[i + 1] == '/')
{
IsComment = true;
i--;
}
else if (AllSource[i] == '"')
IsString = true;
else if (char.IsLetterOrDigit(AllSource[i]) && AllSource[i] != '"')
{
i--;
IsToken = true;
}
else if (char.IsNumber(AllSource[i]))
IsNumber = true;
else if (AllSource[i] == '&' && AllSource[i + 1] == '&')
{
IsToken = true;
i++;
}
else if (AllSource[i] == '[' || AllSource[i] == '<')
{
IsToken = true;
i--;
}
else if (AllSource[i].ToString() == "'" || AllSource[i] == '=')
{
IsToken = true;
i--;
}
else if (AllSource[i] == '@')
{
IsToken = true;
i--;
}
else if (AllSource[i] == ' ') { }
else
{
if (AllSource[i] == '\n' || AllSource[i] == '\r')
{
line++;
column = 0;
}
}
}
if (padr.Length > 0)
{
break;
}
else if (token.Length > 0)
{
padr = token;
EOF = true;
break;
}
}
return padr;
}
}
public class Parse : Lexical
{
int wC = 0; // just for languageBase
public Parse()
{
FileName = include_dir + "langBase.in";
stream = File.OpenRead(FileName);
sr = new StreamReader(stream);
AllSource = sr.ReadToEnd();
counter = 0;
EOF = false;
WordCount();
ParseBase();
}
public bool ParseSourceCode()
{
// Parse all files inputed now
foreach (string files in args)
{
FileName = files;
Console.WriteLine("Compiling: {0}", FileName);
if (!ParseSource())
{
Console.WriteLine("Aborted...");
return false;
}
}
return true;
}
#region PseudoAST
private bool ParseSource()
{
stream = File.OpenRead(FileName);
sr = new StreamReader(stream);
AllSource = sr.ReadToEnd();
counter = 0;
EOF = false;
if (!ReadSourceAndSeparete()) { return false; }
return true;
}
private bool ReadSourceAndSeparete()
{
bool AddWhait = false;
WordCount();
#if DEBUG
foreach (KeyValuePair<string, bool> pair in SyntaxFile)
{
Console.WriteLine("{0}", pair.Key);
}
Console.WriteLine("================= commands =================");
foreach(KeyValuePair<string, Array> pair in SyntaxFileCommands)
{
Console.WriteLine("{0}", pair.Key);
}
#endif
for (int i = 0; i <= wC; i++)
{
string inputToken = ReadWord();
if (inputToken.Length > 0)
{
if (SyntaxFile.ContainsKey(inputToken))
{
// before start the code read
// read the includes
if (FileInclude.Count > 0)
{
if (AddWhait)
{
if (!StopedParse.ContainsKey(FileName))
StopedParse.Add(FileName, counter);
AddWhait = false;
if (!processInclude())
return false;
}
}
switch (inputToken)
{
case "symbol":
if(!ParseSymbol())
return false;
break;
case "definition":
if(!ProcessDefinition())
return false;
break;
}
}
else if (SyntaxFileCommands.ContainsKey(inputToken))
{
switch (inputToken)
{
case "@define":
if (!ParseDefine())
return false;
else
AddWhait = true;
break;
}
}
else
{
Console.WriteLine("It's right -.-");
}
}
}
return true;
}
public bool processInclude()
{
for (int i = 0; i < FileInclude.Count; i++)
{
if (!IsFIleProcessed[i])
{
IsFIleProcessed[i] = true;
FileName = FileInclude[i];
stream = File.OpenRead(FileInclude[i]);
sr = new StreamReader(stream);
AllSource = sr.ReadToEnd();
counter = 0;
EOF = false;
if (!ReadSourceAndSeparete())
{
return false;
}
}
}
if (!RestoreParse())
return false;
return true;
}
private bool RestoreParse()
{
var dictAux = StopedParse.Reverse();
foreach (KeyValuePair<string, int> pair in dictAux)
{
FileName = pair.Key;
stream = File.OpenRead(pair.Key);
sr = new StreamReader(stream);
AllSource = sr.ReadToEnd();
counter = pair.Value;
EOF = false;
if (!ReadSourceAndSeparete())
{
return false;
}
}
return true;
}
private bool ProcessDefinition()
{
return true;
}
private bool ParseSymbol()
{
return true;
}
private bool ParseDefine()
{
string name = ReadWord();
string KeyWordSep = string.Empty;
bool complexParse = false;
// Get DEFINE syntax
string[] commands = SyntaxFileCommands["@define"].OfType<string>().Select(i => i.ToString()).ToArray();
bool[] nNecessaryCommands = new bool[commands.Length];
for (int i = 0; i < commands.Length; i++)
{
if (commands[i][0] == '[')
{
nNecessaryCommands[i] = false;
}
else if (commands[i][0] == '<')
{
nNecessaryCommands[i] = true;
}
else
{
nNecessaryCommands[i] = true;
KeyWordSep = commands[i];
}
}
if (ReadWord() != KeyWordSep)
{
Console.WriteLine("Erro... {0}:{1}", line, column);
return false;
}
// Busca por nNecessaryCommands true
for (int i = 0; i < nNecessaryCommands.Length; i++)
if (nNecessaryCommands[i] == false)
complexParse = true;
if (complexParse) { }
else
{
// eg: std'basic
// std = folder name
// basic = file name + ".in"
// parse the name of new file to parse
string[] sepName = ReadWord().Split('\'');
if (!Directory.Exists(include_dir + sepName[0]))
{
Console.WriteLine("DirectorY: {0} not exist :(");
return false;
}
if (!File.Exists(include_dir + sepName[0] + @"\" + sepName[1] + ".in"))
{
Console.WriteLine("DirectorY: {0} not exist :(");
return false;
}
string fileNameDefine = string.Format(include_dir + sepName[0] + @"\" + sepName[1] + ".in");
if(!FileInclude.Contains(fileNameDefine))
{
FileInclude.Add(fileNameDefine);
IsFIleProcessed.Add(false);
}
}
return true;
}
#endregion
#region ParseBase
private void ParseBase()
{
bool useNameAndType = false;
List<string> words = new List<string>();
for (int i = 0; i <= wC; i++)
{
words.Add(ReadWord());
}
// Parse words and add in special dict
for(int i=0;i<wC;i++)
{
string fncName = words[i];
useNameAndType = false;
if (char.IsLetter(words[i][0]))
{
if (words[i + 1][0] == '[')
{
if (words[i + 2][0] == '<')
{
useNameAndType = true;
}
i+=2;
}
else if (words[i + 1][0] == '<')
{
useNameAndType = false;
i++;
}
else
Console.WriteLine("Erro &&...??");
SyntaxFile.Add(fncName, useNameAndType);
}
else if (words[i][0] == '@')
{
List<string> commands = new List<string>();
bool CheckNextCommand = false;
fncName = words[i];
// parse instructions
if (words[i + 1][0] == '[')
{
commands.Add(words[i + 1]);
if (words[i + 2] == "=")
{
commands.Add(words[i + 2]);
CheckNextCommand = true;
}
}
else if (words[i + 1][0] == '<')
{
commands.Add(words[i + 1]);
if (words[i + 2] == "=")
{
commands.Add(words[i + 2]);
CheckNextCommand = true;
}
}
else
Console.WriteLine("Erro @...??");
if (CheckNextCommand)
{
commands.Add(words[i + 3]);
}
string[] lstToArr = commands.ToArray<string>();
SyntaxFileCommands.Add(fncName, lstToArr);
}
}
}
private void WordCount()
{
wC = 0;
string token = string.Empty;
for (int i = 0; i < AllSource.Length; i++)
{
if (AllSource[i] == ' ' || AllSource[i] == '\n' || AllSource[i] == '\r')
{
if (token.Length > 0)
{
token = string.Empty;
wC++;
}
token = string.Empty;
}
else
{
token += AllSource[i];
}
}
wC++;
}
#endregion
}
class Program
{
static void Main(string[] args)
{
Parse parse = new Parse();
parse.args = args;
if (!parse.ParseSourceCode())
Console.WriteLine(":(");
else
Console.WriteLine(":)");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.