MainClass

This commit is contained in:
Timerix22 2023-12-22 22:43:02 +06:00
parent eed8d26712
commit f27939ee67
11 changed files with 38 additions and 46 deletions

View File

@ -1,16 +1,5 @@
public static class Calculator public static class Calculator {
{ public static double Calculate(string exprStr) {
public static void Main(string[] args){
string() joined = "";
foreach(string arg in args){
joined += arg;
}
double rezult = Calculate(joined);
Console.WriteLine(rezult);
}
public static double Calculate(string exprStr)
{
Lexer() lexer; Lexer() lexer;
List<Token()> tokens = lexer.Lex(exprStr); List<Token()> tokens = lexer.Lex(exprStr);
// lexer.PrintTokens(); // lexer.PrintTokens();

View File

@ -1,5 +1,4 @@
abstract class FunctionCallExpression : IExpression abstract class FunctionCallExpression : IExpression {
{
IExpression X; IExpression X;
internal void Init!(IExpression x){ internal void Init!(IExpression x){

View File

@ -1,4 +1,3 @@
abstract class IExpression abstract class IExpression {
{
internal abstract double Calculate(); internal abstract double Calculate();
} }

View File

@ -1,5 +1,4 @@
class NumericExpression : IExpression class NumericExpression : IExpression {
{
double N; double N;
internal void Init!(double n){ internal void Init!(double n){

View File

@ -1,5 +1,4 @@
abstract class OperatorExpression : IExpression abstract class OperatorExpression : IExpression {
{
IExpression A; IExpression A;
IExpression B; IExpression B;

View File

@ -1,7 +1,5 @@
class Lexer class Lexer {
{
string ExprStr; string ExprStr;
List<Token()>() TokenStorage; List<Token()>() TokenStorage;
int tokType = Token.Type_Number; int tokType = Token.Type_Number;
int tokBegin = 0; int tokBegin = 0;
@ -39,10 +37,8 @@ class Lexer
internal List<Token()> Lex!(string exprStr){ internal List<Token()> Lex!(string exprStr){
ExprStr = exprStr; ExprStr = exprStr;
while (i < ExprStr.Length) while (i < ExprStr.Length) {
{ switch (ExprStr[i]) {
switch (ExprStr[i])
{
// end token, add new predifined token and move next // end token, add new predifined token and move next
case '(': AddStaticToken(TokBracketOpen); break; case '(': AddStaticToken(TokBracketOpen); break;
case ')': AddStaticToken(TokBracketClose); break; case ')': AddStaticToken(TokBracketClose); break;
@ -75,10 +71,8 @@ class Lexer
return TokenStorage; return TokenStorage;
} }
void TryEndToken!() void TryEndToken!() {
{ if (tokBegin != i) {
if (tokBegin != i)
{
Token() tok = Token.Create(ExprStr, tokBegin, i - tokBegin, tokType); Token() tok = Token.Create(ExprStr, tokBegin, i - tokBegin, tokType);
TokenStorage.Add(); TokenStorage.Add();
TokenStorage[TokenStorage.Count-1] = tok; TokenStorage[TokenStorage.Count-1] = tok;
@ -87,8 +81,7 @@ class Lexer
} }
} }
void AddStaticToken!(Token() tok) void AddStaticToken!(Token() tok) {
{
TryEndToken(); TryEndToken();
TokenStorage.Add(); TokenStorage.Add();
TokenStorage[TokenStorage.Count-1] = tok; TokenStorage[TokenStorage.Count-1] = tok;

15
src/MainClass.fu Normal file
View File

@ -0,0 +1,15 @@
public static class MainClass {
public static void Main(string[] args){
#if CS
native {
System.Globalization.CultureInfo.DefaultThreadCurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
}
#endif
string() joined = "";
foreach(string arg in args){
joined += arg;
}
double rezult = Calculator.Calculate(joined);
Console.WriteLine(rezult);
}
}

View File

@ -1,19 +1,18 @@
class Parser class Parser {
{
List<Token()> TokenStorage; List<Token()> TokenStorage;
IExpression# RootExpression; IExpression# RootExpression;
TokenLinkedList() TokensInRPN; TokenLinkedList() TokensInRPN;
Token() TokZero; Token() TokZero;
internal Parser(){ internal Parser(){
NumericExpression# nan = new NumericExpression(); RootExpression = new NumericExpression(); // NaN
nan.Init(Math.NaN);
RootExpression = nan;
TokZero = Token.Create("0", 0, 1, Token.Type_Number); TokZero = Token.Create("0", 0, 1, Token.Type_Number);
} }
internal IExpression# Parse!(List<Token()> tokens){ internal IExpression# Parse!(List<Token()> tokens){
TokenStorage = tokens; TokenStorage = tokens;
if(tokens.Count == 0)
return RootExpression; // NaN
SortTokensInRPN(); SortTokensInRPN();
BuildExpressionTree(); BuildExpressionTree();
return RootExpression; return RootExpression;
@ -133,6 +132,7 @@ class Parser
if(expressionStack.Count == 1) if(expressionStack.Count == 1)
RootExpression = expressionStack.Pop(); RootExpression = expressionStack.Pop();
else RootExpression = new NumericExpression(); // NaN
} }
// returns the number or NaN // returns the number or NaN

View File

@ -1,12 +1,11 @@
class Token class Token {
{
string Str; string Str;
int StartIndex; int StartIndex;
int Length; int Length;
// It is not a enum because there is no way to convert enum value to int in Fusion. // It is not a enum because there is no way to convert enum value to int in Fusion.
// The Type is also the priority of the token in calculation (see Parser). // The Type is also the priority of the token in calculation (see Parser).
int Type; int Type;
public const int Type_OperatorPow=10; public const int Type_OperatorPow=10;
public const int Type_OperatorMul=9; public const int Type_OperatorMul=9;
public const int Type_OperatorMod=8; public const int Type_OperatorMod=8;

View File

@ -43,7 +43,7 @@ class TokenLinkedList {
} }
// Can be used by GraphViz to create node graph. // Can be used by GraphViz to create node graph.
// GraphViz online: https://dreampuf.github.io/GraphvizOnline/ // GraphViz online: https://dreampuf.github.io/GraphvizOnline/
internal string() ToGraphVizCode() { internal string() ToGraphVizCode() {
string() s = "strict digraph {\n" + /* directed graph with no multi-edges */ string() s = "strict digraph {\n" + /* directed graph with no multi-edges */
" start [shape=Mdiamond];\n" + /* 'start' node style */ " start [shape=Mdiamond];\n" + /* 'start' node style */
@ -73,4 +73,4 @@ class TokenLinkedList {
s += "}"; s += "}";
return s; return s;
} }
} }

View File

@ -23,4 +23,4 @@ class TokenLinkedListNode {
Prev = next.GetPrev(); Prev = next.GetPrev();
next.SetPrev(this); next.SetPrev(this);
} }
} }