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 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)
{
public static class Calculator {
public static double Calculate(string exprStr) {
Lexer() lexer;
List<Token()> tokens = lexer.Lex(exprStr);
// lexer.PrintTokens();

View File

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

View File

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

View File

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

View File

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

View File

@ -1,7 +1,5 @@
class Lexer
{
class Lexer {
string ExprStr;
List<Token()>() TokenStorage;
int tokType = Token.Type_Number;
int tokBegin = 0;
@ -39,10 +37,8 @@ class Lexer
internal List<Token()> Lex!(string exprStr){
ExprStr = exprStr;
while (i < ExprStr.Length)
{
switch (ExprStr[i])
{
while (i < ExprStr.Length) {
switch (ExprStr[i]) {
// end token, add new predifined token and move next
case '(': AddStaticToken(TokBracketOpen); break;
case ')': AddStaticToken(TokBracketClose); break;
@ -75,10 +71,8 @@ class Lexer
return TokenStorage;
}
void TryEndToken!()
{
if (tokBegin != i)
{
void TryEndToken!() {
if (tokBegin != i) {
Token() tok = Token.Create(ExprStr, tokBegin, i - tokBegin, tokType);
TokenStorage.Add();
TokenStorage[TokenStorage.Count-1] = tok;
@ -87,8 +81,7 @@ class Lexer
}
}
void AddStaticToken!(Token() tok)
{
void AddStaticToken!(Token() tok) {
TryEndToken();
TokenStorage.Add();
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;
IExpression# RootExpression;
TokenLinkedList() TokensInRPN;
Token() TokZero;
internal Parser(){
NumericExpression# nan = new NumericExpression();
nan.Init(Math.NaN);
RootExpression = nan;
RootExpression = new NumericExpression(); // NaN
TokZero = Token.Create("0", 0, 1, Token.Type_Number);
}
internal IExpression# Parse!(List<Token()> tokens){
TokenStorage = tokens;
if(tokens.Count == 0)
return RootExpression; // NaN
SortTokensInRPN();
BuildExpressionTree();
return RootExpression;
@ -133,6 +132,7 @@ class Parser
if(expressionStack.Count == 1)
RootExpression = expressionStack.Pop();
else RootExpression = new NumericExpression(); // NaN
}
// returns the number or NaN

View File

@ -1,12 +1,11 @@
class Token
{
class Token {
string Str;
int StartIndex;
int Length;
// 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;
public const int Type_OperatorPow=10;
public const int Type_OperatorMul=9;
public const int Type_OperatorMod=8;

View File

@ -43,7 +43,7 @@ class TokenLinkedList {
}
// 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() {
string() s = "strict digraph {\n" + /* directed graph with no multi-edges */
" start [shape=Mdiamond];\n" + /* 'start' node style */
@ -73,4 +73,4 @@ class TokenLinkedList {
s += "}";
return s;
}
}
}

View File

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