diff --git a/CLI/CommandNS/InfoCommand.cs b/CLI/CommandNS/InfoCommand.cs index cf6c9d2311083cf98382ff99a5b8f3bf889c6e82..9e29556970e58479fb6f5ab05c1b03931373817c 100644 --- a/CLI/CommandNS/InfoCommand.cs +++ b/CLI/CommandNS/InfoCommand.cs @@ -19,7 +19,6 @@ namespace CLI.CommandNS if (args.Length < 2) { Help(); return; } string infile = args[1]; Interpreter interpreter = new Interpreter(infile); - interpreter.InitXMLFile(infile); Console.WriteLine($"{"File:",-10}{infile}"); Console.WriteLine(); diff --git a/CLI/CommandNS/RunCommand.cs b/CLI/CommandNS/RunCommand.cs index caf62e5848739e846e801e4a3d983787c10fd0e2..0c90685273ee03c253d7166676cb8b734a010ce2 100644 --- a/CLI/CommandNS/RunCommand.cs +++ b/CLI/CommandNS/RunCommand.cs @@ -1,19 +1,104 @@ -namespace CLI.CommandNS +using CLI.FileWorker; +using CLI.OperationsNS; +using Core; + +namespace CLI.CommandNS { public class RunCommand : AbstractCommand { public override string Name => "Run"; public override string Description => "Description for Run command."; + private FileManager inputFile; + private bool Time = false; + private bool NoOutput = false; + private EOperationType operation = EOperationType.NONE; public override void Help() { - // TODO - Console.WriteLine("How to use Help command"); + Console.WriteLine("The Run command executes the program from provided file."); + Console.WriteLine(""); + Console.WriteLine($"Syntax: CLI.exe run <inputfile> [-add|-override|-replace <libfile> ...] [-noOutput] [-time]"); + Console.WriteLine(""); + Console.WriteLine("It is possible to add/replace/override multiple libraries."); + Console.WriteLine("The order of libraries does matter, they are merged from left to right."); + Console.WriteLine("The Main function from libraries is always ignored."); + Console.WriteLine(""); + Console.WriteLine($" {"-add",-10}Adds functions from library to main.\n{" ",12}When adding an existing function, nothing happens."); + Console.WriteLine(""); + Console.WriteLine($" {"-replace",-10}Replaces the functions from main by library functions.\n{" ",12}When replacing a non-existing function, the function is added."); + Console.WriteLine(""); + Console.WriteLine($" {"-override",-10}Similar to replace, but function signature must match."); + Console.WriteLine(""); + Console.WriteLine($" {"-noOutput",-10}Program will not produce any output."); + Console.WriteLine(""); + Console.WriteLine($" {"-time",-10}Measures time in microseconds."); + Console.WriteLine(""); + } public override void Execute(string[] args) { - // TODO + if (args.Length < 2) { Help(); return; } + + inputFile = new FileManager(args[1]); + + if (!inputFile.LoadFile()) { Help(); return; } + + for (int i = 2; i < args.Length; i++) + { + switch (args[i]) + { + case "-time": + Time = true; + operation = EOperationType.NONE; + break; + case "-noOutput": + NoOutput = true; + operation = EOperationType.NONE; + break; + case "-add": + operation = EOperationType.ADD; + break; + case "-override": + operation = EOperationType.OVERRIDE; + break; + case "-replace": + operation = EOperationType.REPLACE; + break; + default: + if (!processLib(args[i])) { return; } + break; + } + } + + Interpreter interpreter = new Interpreter(inputFile.doc); + interpreter.Run(NoOutput, Time); + } + + private bool processLib(string fileName) + { + FileManager library = new FileManager(fileName); + if (!library.LoadFile()) + { + Help(); + return false; + } + switch (operation) + { + case EOperationType.NONE: + Help(); + return false; + case EOperationType.ADD: + Operations.Add(inputFile.doc, library.doc); + break; + case EOperationType.OVERRIDE: + Operations.Override(inputFile.doc, library.doc); + break; + case EOperationType.REPLACE: + Operations.Replace(inputFile.doc, library.doc); + break; + } + return true; } } } diff --git a/Core/CodeNS/XMLParser.cs b/Core/CodeNS/XMLParser.cs index bd801525208f8c6b63c4d2156cb54ea817c0f5ac..c66dbf610228be099e417ec099c55eb1e7afa11a 100644 --- a/Core/CodeNS/XMLParser.cs +++ b/Core/CodeNS/XMLParser.cs @@ -11,7 +11,8 @@ namespace Core.CodeNS { public static class XmlParser { - public static bool debbugMode = false; + public static bool DebbugMode = false; + public static bool NoOutput = false; private static Dictionary<string, string> ExpressionF = new Dictionary<string, string>() { {"abs", "ABS"}, @@ -285,7 +286,7 @@ namespace Core.CodeNS "do" => () => queue.Enqueue(HandleDo(statement)), "break" => () => queue.Enqueue(HandleBreak(statement)), "continue" => () => queue.Enqueue(HandleContinue(statement)), - "breakpoint" => () => { if (debbugMode) { queue.Enqueue(HandleBreakpoint(statement)); }}, + "breakpoint" => () => { if (DebbugMode) { queue.Enqueue(HandleBreakpoint(statement)); }}, _ => () => Console.WriteLine("Spatny statement") }; action.Invoke(); diff --git a/Core/Interpreter.cs b/Core/Interpreter.cs index 70f5d3df82cf7015d2e734665fe6429a6c6c2473..99c7772dbd9d0756a415f69f6a58d3b7d433e4a4 100644 --- a/Core/Interpreter.cs +++ b/Core/Interpreter.cs @@ -10,73 +10,98 @@ public class Interpreter { - private bool RunTests = false; - private int NumberOfTests = 21; - private readonly string InputFile; - public Dictionary<string, string> ProgramAttributes { get; set; } - - public Interpreter(string inputFile) => - (InputFile, ProgramAttributes) = (inputFile, new Dictionary<string, string>()); - - public void Run(string[]? args) + private string InputFile; + private XDocument InputDocument; + private bool succesfullyCreated; + public Dictionary<string, string> ProgramAttributes { - if (RunTests) + get { - for (int i = 1; i <= NumberOfTests; i++) - { - RunTest(i); - } + return XmlParser.InitAttributes(); } - else - { - if (!InitXMLFile(InputFile)) - { - Console.WriteLine("Error :("); - } + } - XmlParser.createMainFunction(); + public Interpreter() => succesfullyCreated = false; + public Interpreter(string inputFile) + { + InputFile = inputFile; + succesfullyCreated = InitXMLParser(); + } + public Interpreter(XDocument inputDocument) + { + InputDocument = inputDocument; + succesfullyCreated = InitXMLParser(); + } + + public void Run(bool noOutput, bool time) + { + if (succesfullyCreated) + { + long startTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; + XmlParser.NoOutput = noOutput; + XmlParser.createMainFunction(); while (true) { Function f = Memory.peekFunction(); if (f == null) { - Console.WriteLine("END OF CODE"); break; } f.Step(); } + long endTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; + if (time) { Console.WriteLine("Time: " + (endTime - startTime) + " milliseconds"); } } } - public bool InitXMLFile(string xmlFile) + public void RunInternTests() { - if (!File.Exists(xmlFile)) + int NumberOfTests = 21; + for (int i = 1; i <= NumberOfTests; i++) { - Console.WriteLine("File: " + xmlFile + " does not exist."); - return false; + string testFileName = "test_" + i + ".fprg"; + InputFile = Directory.GetCurrentDirectory() + "\\Core\\Tests\\" + testFileName; + succesfullyCreated = InitXMLParser(); + Console.Write(testFileName + ": "); + Run(false, false); } + } - XDocument doc = XDocument.Load(xmlFile); - var schema = new XmlSchemaSet(); - schema.Add("", "fprg.xsd"); - - doc.Validate(schema, (xdoc, err) => + private bool InitXMLParser() + { + if (InputFile != null) { - throw new Exception(err.Message); - }); + if (!File.Exists(InputFile)) + { + Console.WriteLine("File: " + InputFile + " does not exist."); + return false; + } + InputDocument = XDocument.Load(InputFile); + } - if (doc.Root == null) + if (InputDocument.Root == null) { - Console.WriteLine("Cannot load file: " + xmlFile); + Console.WriteLine("Cannot load XDocument: " + InputFile); return false; } + validateXDocument(); - XmlParser.Init(doc.Root); - ProgramAttributes = XmlParser.InitAttributes(); + XmlParser.Init(InputDocument.Root); return true; } + private void validateXDocument() + { + var schema = new XmlSchemaSet(); + schema.Add("", "fprg.xsd"); + + InputDocument.Validate(schema, (xdoc, err) => + { + throw new Exception(err.Message); + }); + } + public void PrintHeap() { Console.WriteLine("---------------------\nHalda\n---------------------"); @@ -99,29 +124,5 @@ } } } - - public void RunTest(int TestNumber) - { - string fileName = Directory.GetCurrentDirectory() + "\\Core\\Tests\\test_" + TestNumber + ".fprg"; - - if (!InitXMLFile(fileName)) - { - Console.WriteLine("Error :("); - } - - Console.Write("test_" + TestNumber + ".fprg: "); - - XmlParser.createMainFunction(); - - while (true) - { - Function f = Memory.peekFunction(); - if (f == null) - { - break; - } - f.Step(); - } - } } } diff --git a/Core/StatementNS/AStatement.cs b/Core/StatementNS/AStatement.cs index b5a3f10a60b854d4e9ebfac01796b64289d4310c..acb0712e3f6c4e7c61112ca3023e927fc209347f 100644 --- a/Core/StatementNS/AStatement.cs +++ b/Core/StatementNS/AStatement.cs @@ -1,4 +1,5 @@ -using Core.DataTypeNS; +using Core.CodeNS; +using Core.DataTypeNS; using Core.ExpressionSolverNS; using Core.MemoryNS; using Array = Core.DataTypeNS.Array; @@ -142,6 +143,10 @@ namespace Core.StatementNS public override bool Execute(Function function) { + if (XmlParser.NoOutput) + { + return true; + } if (exSolver == null) { exSolver = new ExpressionSolver(Expression); diff --git a/test.fprg b/test.fprg index 1bdce3c8031accf42796a58bcb832955c2850ec4..fb93903fe860b2f1f7edfa653e6da5ec68ef1676 100644 --- a/test.fprg +++ b/test.fprg @@ -11,7 +11,7 @@ <function name="Main" type="None" variable=""> <parameters/> <body> - <declare name="numbers, hehe, ok" type="Integer"/> + <declare name="number, sum" type="Integer"/> <assign variable="number" expression="10"/> <assign variable="sum" expression="a() + b()"/> <output expression="sum" newline="True"/>