Example #1
0
        private void RunButton_Click(object sender, EventArgs e)
        {
            OutputPanel.Controls.Clear();
            Console.WriteLine("Running Aphid");
            var interpreter = new AphidInterpreter();

            interpreter.CurrentScope.Add("root", ValueHelper.Wrap(OutputPanel));

            try
            {
                interpreter.Interpret(CodeTextBox.Text);
            }
            catch (AphidParserException exception)
            {
                Console.WriteLine("Parser exception\r\n");
                Console.WriteLine(ParserErrorMessage.Create(CodeTextBox.Text, exception, true));
            }
            catch (AphidRuntimeException exception)
            {
                Console.WriteLine("Unexpected runtime exception\r\n\r\n{0}\r\n", exception.Message);
                DumpStackTrace(interpreter);
            }
            catch (Exception exception)
            {
                Console.WriteLine(
                    "Unexpected exception\r\n\r\n{0}\r\n",
                    ExceptionHelper.Unwrap(exception).Message);

                DumpStackTrace(interpreter);
            }
            //CodeTextBox.Text
        }
Example #2
0
        private static void Compile(string filename, string outputPath)
        {
            var dir = Path.GetDirectoryName(Path.GetFullPath(filename));

            Directory.SetCurrentDirectory(dir);
            Cli.WriteLine("Compiling ~Cyan~{0}~R~", filename);
            List <AphidExpression> ast;
            var code = File.ReadAllText(filename);

            try
            {
                ast = AphidParser.Parse(code, useImplicitReturns: false);
            }
            catch (AphidParserException e)
            {
                Console.WriteLine(ParserErrorMessage.Create(code, e, true));
                Console.ReadKey();

                return;
            }

            var emitter = new AphidVerilogEmitter();

            //var mutatorGroup = new AphidMutatorGroup();

            foreach (var m in new AphidMutator[]
            {
                new IncludeMutator(dir, false),
                new AphidMacroMutator(),
                new ConstantFoldingMutator(),
                new ForUnrollMutator(),
                new DynamicIdentifierMutator(),
            })
            {
                ast = m.MutateRecursively(ast);
            }

            //ast = mutatorGroup.MutateRecursively(ast);
            var verilog = emitter.Emit(ast);
            var outFile = Path.GetFileNameWithoutExtension(filename) + ".v";

            if (outputPath == null)
            {
                outputPath = Path.GetDirectoryName(filename);
            }

            outFile = Path.Combine(outputPath, outFile);
            Cli.WriteLine("Writing Verilog to ~Cyan~{0}~R~", outFile);
            File.WriteAllText(outFile, verilog);
            Cli.WriteLine("~Green~Done~R~");
        }
Example #3
0
        private List <AphidExpression> ParseCode(string filename)
        {
            var code = File.ReadAllText(filename);

            try
            {
                return(AphidParser.Parse(code, isTextDocument: _isText));
            }
            catch (AphidParserException exception)
            {
                var msg = ParserErrorMessage.Create(code, exception, true);
                Cli.WriteCriticalErrorMessage("~Yellow~Error parsing code~R~\r\n\r\n{0}", Cli.Escape(msg));
                Environment.Exit(100);
                throw;
            }
        }
Example #4
0
        public List <AphidExpression> Parse(Response response, string file)
        {
            var code = File.ReadAllText(file);

            try
            {
                return(MutatorGroups.GetStandard().Mutate(AphidParser.Parse(code, file)));
            }
            catch (AphidParserException e)
            {
                SendErrorResponse(
                    response,
                    0x523000,
                    ParserErrorMessage.Create(code, e, highlight: false));

                return(null);
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            var code = @"
                #'std';
                print('foo'bar');
            ";

            try
            {
                var interpreter = new AphidInterpreter();
                interpreter.Interpret(code);
            }
            catch (AphidParserException e)
            {
                var msg = ParserErrorMessage.Create(code, e, true);
                Console.WriteLine(msg);
            }
        }
Example #6
0
 public static string Format(AphidParserException exception, string code) =>
 $"Unhandled parser exception: {ParserErrorMessage.Create(code, exception, true)}";
Example #7
0
File: App.cs Project: 5l1v3r1/Aphid
        private void ValidateTextDocument(TextDocumentItem document)
        {
            _text = document.text;

            //Proxy.TextDocument.PublishDiagnostics(new PublishDiagnosticsParams
            //{
            //    uri = document.uri,
            //    diagnostics = new Diagnostic[0],

            //});

            //return;

            //Logger.Instance.Log("Validate " + document.text);
            Logger.Instance.Log("Validate");

            Diagnostic[]           diagnostics;
            List <AphidExpression> ast = null;

            try
            {
                ast         = AphidParser.Parse(document.text);
                diagnostics = new Diagnostic[0];
                Logger.Instance.Log("Success");
            }
            catch (AphidParserException e)
            {
                Logger.Instance.Log("Failure: " + e.ToString());
                var pos = TokenHelper.GetIndexPosition(document.text, e.UnexpectedToken.Index);

                if (pos != null)
                {
                    Logger.Instance.Log(pos.ToString());
                }
                else
                {
                    Logger.Instance.Log("No position");
                }

                diagnostics = new[]
                {
                    new Diagnostic
                    {
                        severity = DiagnosticSeverity.Error,
                        range    = new Range
                        {
                            start = pos != null ?
                                    new Position
                            {
                                line      = pos.Item1,
                                character = pos.Item2
                            } :
                            new Position(),
                            end = pos != null ?
                                  new Position
                            {
                                line      = pos.Item1,
                                character =
                                    pos.Item2 + e.UnexpectedToken.Lexeme != null &&
                                    e.UnexpectedToken.Lexeme.Length > 0 ?
                                    e.UnexpectedToken.Lexeme.Length : 0
                            } :
                            new Position(),
                        },
                        message = ParserErrorMessage.Create(document.text, e, false),
                        source  = "aphid"
                    }
                };
            }

            Proxy.TextDocument.PublishDiagnostics(new PublishDiagnosticsParams
            {
                uri         = document.uri,
                diagnostics = diagnostics
            });
        }
Example #8
0
        public int Generate(
            string wszInputFilePath,
            string bstrInputFileContents,
            string wszDefaultNamespace,
            IntPtr[] rgbOutputFileContents,
            out uint pcbOutput,
            IVsGeneratorProgress pGenerateProgress)
        {
            if (bstrInputFileContents == null)
            {
                throw new ArgumentException(bstrInputFileContents);
            }

            string csOut  = null;
            var    result = VSConstants.E_FAIL;

            try
            {
#if RELEASE
                if (AphidBuildInterop.Compile == null)
                {
                    var interpreter = new AphidInterpreter();

                    var interopScript = Path.Combine(
                        Path.GetDirectoryName(typeof(AphidExpression).Assembly.Location),
                        "AphidBuildInterop.alx");

                    interpreter.InterpretFile(interopScript);
                }
#else
                _interopScript.Refresh();

                if (_lastRun < _interopScript.LastWriteTime)
                {
                    _lastRun = DateTime.Now;
                    var interpreter = new AphidInterpreter();
                    interpreter.InterpretFile(_interopScript.FullName);
                }
#endif

                csOut = AphidBuildInterop.Compile(
                    wszInputFilePath,
                    bstrInputFileContents,
                    wszDefaultNamespace);

                result = VSConstants.S_OK;
            }
            catch (AphidParserException e)
            {
                if (e.UnexpectedToken.TokenType != default(AphidToken).TokenType)
                {
                    var pos = TokenHelper.GetIndexPosition(
                        bstrInputFileContents,
                        e.UnexpectedToken.Index);

                    pGenerateProgress.GeneratorError(
                        0,
                        0,
                        ParserErrorMessage.Create(bstrInputFileContents, e, false),
                        pos == null || pos.Item1 == -1 ? 0xffffffffu : (uint)pos.Item1,
                        pos == null || pos.Item2 == -1 ? 0xffffffffu : (uint)pos.Item2);
                }
                else
                {
                    WriteError(
                        pGenerateProgress,
                        bstrInputFileContents,
                        e,
                        e.Expression);
                }
            }
            catch (AphidLoadScriptException e)
            {
                WriteError(
                    pGenerateProgress,
                    bstrInputFileContents,
                    e,
                    e.CurrentExpression ?? e.CurrentStatement);
            }
            catch (AphidRuntimeException e)
            {
                WriteError(
                    pGenerateProgress,
                    bstrInputFileContents,
                    e,
                    e.CurrentExpression ?? e.CurrentStatement);
            }
            catch (Exception e)
            {
                WriteError(
                    pGenerateProgress,
                    bstrInputFileContents,
                    e);
            }

            var bytes = csOut?.GetBytes();

            if (bytes == null || bytes.Length == 0)
            {
                rgbOutputFileContents[0] = IntPtr.Zero;
                pcbOutput = 0;
            }
            else
            {
                rgbOutputFileContents[0] = Marshal.AllocCoTaskMem(bytes.Length);
                Marshal.Copy(bytes, 0, rgbOutputFileContents[0], bytes.Length);
                pcbOutput = (uint)bytes.Length;
            }

            //pGenerateProgress.GeneratorError(

            return(result);
        }