private readonly IReadOnlyDictionary <string, string> _scripts; // lowercase script name -> script code

        public ScriptRunner(INotebook notebook, IReadOnlyDictionary <string, string> scripts)
        {
            _notebook    = notebook;
            _scripts     = scripts;
            _stmtRunners = new Dictionary <Type, Action <Ast.Stmt, ScriptEnv> > {
                [typeof(Ast.SqlStmt)]       = (s, e) => ExecuteSqlStmt((Ast.SqlStmt)s, e),
                [typeof(Ast.DeclareStmt)]   = (s, e) => ExecuteDeclareStmt((Ast.DeclareStmt)s, e),
                [typeof(Ast.SetStmt)]       = (s, e) => ExecuteSetStmt((Ast.SetStmt)s, e),
                [typeof(Ast.IfStmt)]        = (s, e) => ExecuteIfStmt((Ast.IfStmt)s, e),
                [typeof(Ast.WhileStmt)]     = (s, e) => ExecuteWhileStmt((Ast.WhileStmt)s, e),
                [typeof(Ast.ForStmt)]       = (s, e) => ExecuteForStmt((Ast.ForStmt)s, e),
                [typeof(Ast.BlockStmt)]     = (s, e) => ExecuteBlockStmt((Ast.BlockStmt)s, e),
                [typeof(Ast.BreakStmt)]     = (s, e) => ExecuteBreakStmt((Ast.BreakStmt)s, e),
                [typeof(Ast.ContinueStmt)]  = (s, e) => ExecuteContinueStmt((Ast.ContinueStmt)s, e),
                [typeof(Ast.PrintStmt)]     = (s, e) => ExecutePrintStmt((Ast.PrintStmt)s, e),
                [typeof(Ast.ExecuteStmt)]   = (s, e) => ExecuteExecuteStmt((Ast.ExecuteStmt)s, e),
                [typeof(Ast.ReturnStmt)]    = (s, e) => ExecuteReturnStmt((Ast.ReturnStmt)s, e),
                [typeof(Ast.ThrowStmt)]     = (s, e) => ExecuteThrowStmt((Ast.ThrowStmt)s, e),
                [typeof(Ast.RethrowStmt)]   = (s, e) => ExecuteRethrowStmt((Ast.RethrowStmt)s, e),
                [typeof(Ast.TryCatchStmt)]  = (s, e) => ExecuteTryCatchStmt((Ast.TryCatchStmt)s, e),
                [typeof(Ast.ImportCsvStmt)] = (s, e) => ExecuteImportCsvStmt((Ast.ImportCsvStmt)s, e),
                [typeof(Ast.ImportTxtStmt)] = (s, e) => ExecuteImportTxtStmt((Ast.ImportTxtStmt)s, e),
                [typeof(Ast.ExportTxtStmt)] = (s, e) => ExecuteExportTxtStmt((Ast.ExportTxtStmt)s, e),
                [typeof(Ast.ImportXlsStmt)] = (s, e) => ExecuteImportXlsStmt((Ast.ImportXlsStmt)s, e)
            };
        }
Example #2
0
 public void Construtor(INotebook notebookBuilder)
 {
     notebookBuilder.BuildCPU();
     notebookBuilder.BuildMemoria();
     notebookBuilder.BuildTela();
     notebookBuilder.BuildVGA();
 }
        private ExportTxtStmtRunner(INotebook notebook, ScriptEnv env, ScriptRunner runner, Ast.ExportTxtStmt stmt)
        {
            _notebook = notebook;
            _env      = env;
            _runner   = runner;
            _stmt     = stmt;

            _filePath = GetFilePath();

            foreach (var option in _stmt.OptionsList.GetOptionKeys())
            {
                switch (option)
                {
                case "TRUNCATE_EXISTING_FILE ":
                    _truncateExistingFile = _stmt.OptionsList.GetOptionBool(option, _runner, _env, false);
                    break;

                case "FILE_ENCODING":
                    _fileEncoding =
                        _stmt.OptionsList.GetOptionEncoding(option, _runner, _env);
                    break;

                default:
                    throw new Exception($"\"{option}\" is not a recognized option name.");
                }
            }

            if (_fileEncoding == null)
            {
                _fileEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
            }
        }
Example #4
0
        public bool TryLoadFile(string fileName, INotebook notebook)
        {
            try
            {
                using (var fileStream = new FileStream(fileName, FileMode.Open))
                {
                    notebook.RemoveAllNotes();
                    var notes = FileUtils.Deserialize <List <INote> >(fileStream);
                    ThrowIf.Variable.IsNull(notes, nameof(notes));
                    //var notes = (List<INote>) formatter.Deserialize(fileStream);
                    foreach (var note in notes)
                    {
                        notebook.Add(note);
                    }
                }

                _userOutput.WriteMessage("Successfully loaded!");
                return(true);
            }
            catch (SerializationException ex)
            {
                _userOutput.WriteMessage("Failed to deserialize. " + ex.Message);
                return(false);
            }
            catch (FileNotFoundException)
            {
                _userOutput.WriteMessage($"File {fileName} doesn't exist");
                return(false);
            }
        }
Example #5
0
        public bool TrySaveFile(string fileName, INotebook notebook)
        {
            var stream = FileUtils.Serialize(notebook.Notes);

            if (fileName.Length <= 0)
            {
                stream.Dispose();
                return(false);
            }
            try
            {
                using (var fileStream = new FileStream(fileName, FileMode.Create))
                {
                    stream.Position = 0;
                    stream.CopyTo(fileStream);
                }

                _userOutput.WriteMessage("Notebook saved successfully!");
                stream.Dispose();
                return(true);
            }
            catch (SerializationException ex)
            {
                _userOutput.WriteMessage("Failed to serialize. " + ex.Message);
                stream.Dispose();
                return(false);
            }
        }
Example #6
0
        private static T WithTransactionCore <T>(INotebook notebook, Func <T> func, bool rollback)
        {
            var value = default(T);

            WithTransactionCore(notebook, () => {
                value = func();
            }, rollback);
            return(value);
        }
Example #7
0
        public static T WithTransaction <T>(INotebook notebook, Func <T> func)
        {
            var value = default(T);

            WithTransaction(notebook, () => {
                value = func();
            });
            return(value);
        }
Example #8
0
        public override string Execute()
        {
            string    notebookName = base.Parameters[0];
            INotebook notebook     = base.Factory.CreateNotebook(notebookName);

            EngineMaikaTI.LoggedUser.AddNotebook(notebook);
            EngineMaikaTI.CurrentNotebook = notebook;

            return(Messages.NotebookCreated(notebookName));
        }
Example #9
0
        public SaveNoteCommand([NotNull] INotebook notebook, [NotNull] IUserFileInput userFileInput,
                               [NotNull] IFileSaver fileSaver)
        {
            ThrowIf.Variable.IsNull(notebook, nameof(notebook));
            ThrowIf.Variable.IsNull(userFileInput, nameof(userFileInput));
            ThrowIf.Variable.IsNull(fileSaver, nameof(fileSaver));

            _notebook      = notebook;
            _userFileInput = userFileInput;
            _fileSaver     = fileSaver;
        }
Example #10
0
 public TokenQueue(IEnumerable <Token> input, INotebook notebook)
 {
     Notebook = notebook;
     _tokens  = input.ToList();
     if (_tokens.Any())
     {
         _eofLocation = (int)(_tokens.Last().Utf8Start + _tokens.Last().Utf8Length);
     }
     else
     {
         _eofLocation = 0;
     }
 }
Example #11
0
        public static INotebook WCFclient()
        {
            ChannelFactory <INotebook> factory = new ChannelFactory <INotebook>(new WebHttpBinding()
            {
            }, new EndpointAddress(@"http://127.0.0.1:9090/Notebook/"));

            factory.Endpoint.Behaviors.Add(new WebHttpBehavior()
            {
            });
            INotebook proxy = factory.CreateChannel();

            return(proxy);
        }
Example #12
0
        public AddNoteCommand([NotNull] INotebook notebook, [NotNull] IUserInput userInput,
                              [NotNull][ItemNotNull] IReadOnlyList <INoteCreator> creators,
                              [NotNull] IUserOutput userOutput)
        {
            ThrowIf.Variable.IsNull(notebook, nameof(notebook));
            ThrowIf.Variable.IsNull(userInput, nameof(userInput));
            ThrowIf.Variable.IsNull(userOutput, nameof(userOutput));
            ThrowIf.Variable.IsNull(creators, nameof(creators));

            _notebook   = notebook;
            _userInput  = userInput;
            _creators   = creators.ToDictionary(x => x.NoteType.ToLower());
            _userOutput = userOutput;
        }
Example #13
0
        public static void VerifyColumnsExist(string[] colNames, string tableName, INotebook notebook)
        {
            var tableInfo      = notebook.Query($"PRAGMA TABLE_INFO ({tableName.DoubleQuote()})");
            var nameColIndex   = tableInfo.GetIndex("name");
            var actualColNames = tableInfo.Rows.Select(x => x[nameColIndex].ToString().ToLower()).ToList();

            foreach (var name in colNames.Where(x => x != null))
            {
                if (!actualColNames.Contains(name.ToLower()))
                {
                    throw new Exception($"The table \"{tableName}\" does not contain the column \"{name}\".");
                }
            }
        }
Example #14
0
        public static void CreateOrTruncateTable(IReadOnlyList <string> srcColNames, Ast.ImportColumn[] dstColNodes,
                                                 IReadOnlyList <string> dstColNames, string dstTableName, bool temporaryTable, bool truncateExistingTable,
                                                 INotebook notebook)
        {
            // create the table if it doesn't already exist.
            var columnDefs = new List <string>();

            if (dstColNodes.All(x => x == null))
            {
                // the user did not specify a column list, so all columns will be included
                columnDefs.AddRange(srcColNames.Select(SqlUtil.DoubleQuote));
            }
            else
            {
                // the user specified which columns to include
                for (int i = 0; i < dstColNodes.Length; i++)
                {
                    if (dstColNodes[i] != null)
                    {
                        var    name    = dstColNames[i];
                        var    type    = dstColNodes[i].TypeConversion?.ToString() ?? "";
                        string sqlType = "";
                        if (dstColNodes[i].TypeConversion.HasValue)
                        {
                            switch (dstColNodes[i].TypeConversion.Value)
                            {
                            case Ast.TypeConversion.Integer: sqlType = "integer"; break;

                            case Ast.TypeConversion.Real: sqlType = "real"; break;

                            default: sqlType = "text"; break;
                            }
                        }

                        columnDefs.Add($"{name.DoubleQuote()} {sqlType}");
                    }
                }
            }
            var columnDefsList = string.Join(", ", columnDefs);
            var sql            = $"CREATE {(temporaryTable ? "TEMPORARY" : "")} TABLE IF NOT EXISTS " +
                                 $"{dstTableName.DoubleQuote()} ({columnDefsList})";

            notebook.Execute(sql);

            if (truncateExistingTable)
            {
                notebook.Execute($"DELETE FROM {dstTableName.DoubleQuote()}");
            }
        }
Example #15
0
        public NoteSearch(INotebook notebook)
        {
            this.notebook = notebook;

            string indexPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), ".marknote", "index");

            analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48);

            indexDirectoryInfo = new DirectoryInfo(indexPath);

            // TODO - change all console access to log statements
            Console.WriteLine("Index path: {0}", indexPath);

            RebuildIndex(notebook.NoteDir, indexPath);
        }
Example #16
0
        static void Main(string[] args)
        {
            var fabricante = new Fabricante();

            INotebook notebookBuilder = null;

            notebookBuilder = new MacBookBuilder();
            fabricante.Construtor(notebookBuilder);

            Console.WriteLine($"Notebook Construido: {notebookBuilder.Notebook.NomeNotebook}\n" +
                              $"Tela: {notebookBuilder.Notebook.Tela}\n" +
                              $"CPU: {notebookBuilder.Notebook.Cpu}\n" +
                              $"VGA: {notebookBuilder.Notebook.Vga}");
            Console.ReadKey();
        }
        private ImportCsvStmtRunner(INotebook notebook, ScriptEnv env, ScriptRunner runner, Ast.ImportCsvStmt stmt)
        {
            _notebook = notebook;
            _env      = env;
            _runner   = runner;
            _stmt     = stmt;

            foreach (var option in _stmt.OptionsList.GetOptionKeys())
            {
                switch (option)
                {
                case "SKIP_LINES":
                    _skipLines = _stmt.OptionsList.GetOptionLong(option, _runner, _env, 0, minValue: 0);
                    break;

                case "TAKE_LINES":
                    _takeLines = _stmt.OptionsList.GetOptionLong(option, _runner, _env, -1, minValue: -1);
                    break;

                case "HEADER_ROW":
                    _headerRow = _stmt.OptionsList.GetOptionBool(option, _runner, _env, true);
                    break;

                case "TRUNCATE_EXISTING_TABLE":
                    _truncateExistingTable = _stmt.OptionsList.GetOptionBool(option, _runner, _env, false);
                    break;

                case "TEMPORARY_TABLE":
                    _temporaryTable = _stmt.OptionsList.GetOptionBool(option, _runner, _env, false);
                    break;

                case "FILE_ENCODING":
                    _fileEncoding = _stmt.OptionsList.GetOptionEncoding(option, _runner, _env);
                    break;

                case "IF_CONVERSION_FAILS":
                    _ifConversionFails = (IfConversionFails)_stmt.OptionsList.GetOptionLong(
                        option, _runner, _env, 1, minValue: 1, maxValue: 3);
                    break;

                default:
                    throw new Exception($"\"{option}\" is not a recognized option name.");
                }
            }
        }
Example #18
0
 private void DeleteNotebook(INotebook nb)
 {
     if (nb.State == NotebookState.New || System.Windows.MessageBox.Show("Are you sure you wish to remove the notebook: '" + nb.Title + "'?", "OP.Notes", MessageBoxButton.YesNo) ==
         MessageBoxResult.Yes)
     {
         nb.BeforeSave -= notebook_BeforeSave;
         nb.AfterSave  -= notebook_AfterSave;
         _notebooks.Remove(nb);
         nb.Delete();
         var li     = lbNotebooks.Items.OfType <ListBoxItem>().First(ti => ((ucNotebook)ti.Tag).Notebook == nb);
         var ctrlNb = (ucNotebook)li.Tag;
         if (pnlNotebooks.Children.Contains(ctrlNb))
         {
             pnlNotebooks.Children.Remove(ctrlNb);
         }
         lbNotebooks.Items.Remove(li);
     }
 }
Example #19
0
        private ImportTxtStmtRunner(INotebook notebook, ScriptEnv env, ScriptRunner runner, Ast.ImportTxtStmt stmt)
        {
            _notebook = notebook;
            _env      = env;
            _runner   = runner;
            _stmt     = stmt;

            _filePath             = GetFilePath();
            _tableName            = _runner.EvaluateIdentifierOrExpr(_stmt.TableName, _env);
            _lineNumberColumnName = _stmt.LineNumberColumnName == null ? null : _runner.EvaluateIdentifierOrExpr(_stmt.LineNumberColumnName, _env);
            _textColumnName       = _stmt.TextColumnName == null ? null : _runner.EvaluateIdentifierOrExpr(_stmt.TextColumnName, _env);

            foreach (var option in _stmt.OptionsList.GetOptionKeys())
            {
                switch (option)
                {
                case "SKIP_LINES":
                    _skipLines = _stmt.OptionsList.GetOptionLong(option, _runner, _env, 0, minValue: 0);
                    break;

                case "TAKE_LINES":
                    _takeLines = _stmt.OptionsList.GetOptionLong(option, _runner, _env, -1, minValue: -1);
                    break;

                case "TRUNCATE_EXISTING_TABLE":
                    _truncateExistingTable = _stmt.OptionsList.GetOptionBool(option, _runner, _env, false);
                    break;

                case "TEMPORARY_TABLE":
                    _temporaryTable = _stmt.OptionsList.GetOptionBool(option, _runner, _env, false);
                    break;

                case "FILE_ENCODING":
                    _fileEncoding = _stmt.OptionsList.GetOptionEncoding(option, _runner, _env);
                    break;

                default:
                    throw new Exception($"\"{option}\" is not a recognized option name.");
                }
            }
        }
Example #20
0
        private void AddNotebook(INotebook nb, bool editTitle)
        {
            _notebooks.Add(nb);
            var li = new ucNotebookTitle()
            {
                Notebook = nb,
                Tag      = new ucNotebook()
                {
                    RenderTransform = new TranslateTransform(),
                    Notebook        = nb,
                    WordWrap        = toggleWrap.IsChecked.GetValueOrDefault(),
                    ShowTags        = toggleTags.IsChecked.GetValueOrDefault(),
                    TagsOnTop       = toggleTagsOnTop.IsChecked.GetValueOrDefault()
                }
            };

            li.NotebookDeleteRequested += (s, e) => DeleteNotebook(li.Notebook);
            lbNotebooks.Items.Add(li);
            lbNotebooks.SelectedItem = li;
            nb.BeforeSave           += notebook_BeforeSave;
            nb.AfterSave            += notebook_AfterSave;
        }
Example #21
0
        public static void WithTransaction(INotebook notebook, Action action)
        {
            bool didBeginTransaction = false;

            if (!notebook.IsTransactionActive())
            {
                notebook.Execute("BEGIN");
                didBeginTransaction = true;
            }
            try {
                action();
                if (didBeginTransaction)
                {
                    notebook.Execute("COMMIT");
                }
            } catch {
                if (didBeginTransaction)
                {
                    notebook.Execute("ROLLBACK");
                }
                throw;
            }
        }
Example #22
0
        private static void WithTransactionCore(INotebook notebook, Action action, bool rollback)
        {
            var didBeginTransaction = false;

            if (!notebook.IsTransactionActive())
            {
                notebook.Execute("BEGIN");
                didBeginTransaction = true;
            }
            try {
                action();
                if (didBeginTransaction)
                {
                    notebook.Execute(rollback ? "ROLLBACK" : "COMMIT");
                }
            } catch {
                if (didBeginTransaction)
                {
                    notebook.Execute("ROLLBACK");
                }
                throw;
            }
        }
 public TouchscreenDecorator(INotebook notebook)
     : base(notebook)
 {
 }
        // must be run from the SQLite thread
        public static void Run(INotebook notebook, ScriptEnv env, ScriptRunner runner, Ast.ExportTxtStmt stmt)
        {
            var exporter = new ExportTxtStmtRunner(notebook, env, runner, stmt);

            exporter.Export();
        }
Example #25
0
 public void AddNotebook(INotebook notebook)
 {
     this.Notebooks.Add(notebook);
 }
 public NotebookDecorator(INotebook notebook)
 {
     this._notebook = notebook;
 }
        private ImportXlsStmtRunner(INotebook notebook, ScriptEnv env, ScriptRunner runner, Ast.ImportXlsStmt stmt)
        {
            _notebook = notebook;
            _env      = env;
            _runner   = runner;
            _stmt     = stmt;

            _filePath = _runner.EvaluateExpr <string>(_stmt.FilenameExpr, _env);
            if (!File.Exists(_filePath))
            {
                throw new Exception($"The specified XLS/XLSX file was not found: \"{_filePath}\"");
            }

            if (_stmt.WhichSheetExpr != null)
            {
                _whichSheet = _runner.EvaluateExpr(_stmt.WhichSheetExpr, _env);
            }

            int?index;

            foreach (var option in _stmt.OptionsList.GetOptionKeys())
            {
                switch (option)
                {
                case "FIRST_ROW":
                    _firstRowIndex = _stmt.OptionsList.GetOptionInt(option, _runner, _env, 1, minValue: 1) - 1;
                    break;

                case "LAST_ROW":
                    var lastRowNum = _stmt.OptionsList.GetOptionInt(option, _runner, _env, 0, minValue: 0);
                    if (lastRowNum == 0)
                    {
                        _lastRowIndex = null;
                    }
                    else
                    {
                        _lastRowIndex = lastRowNum - 1;
                    }
                    break;

                case "FIRST_COLUMN":
                    index = XlsUtil.ColumnRefToIndex(_stmt.OptionsList.GetOption <object>(option, _runner, _env, null));
                    if (index.HasValue)
                    {
                        _firstColumnIndex = index.Value;
                    }
                    else
                    {
                        throw new Exception($"The {option} option must be a valid column number or string.");
                    }
                    break;

                case "LAST_COLUMN":
                    var lastColumnValue = _stmt.OptionsList.GetOption <object>(option, _runner, _env, null);
                    if (lastColumnValue.Equals(0))
                    {
                        _lastColumnIndex = null;
                        break;
                    }
                    index = XlsUtil.ColumnRefToIndex(lastColumnValue);
                    if (index.HasValue)
                    {
                        _lastColumnIndex = index.Value;
                    }
                    else
                    {
                        throw new Exception($"The {option} option must be a valid column number or string.");
                    }
                    break;

                case "HEADER_ROW":
                    _headerRow = _stmt.OptionsList.GetOptionBool(option, _runner, _env, true);
                    break;

                case "TRUNCATE_EXISTING_TABLE":
                    _truncateExistingTable = _stmt.OptionsList.GetOptionBool(option, _runner, _env, false);
                    break;

                case "TEMPORARY_TABLE":
                    _temporaryTable = _stmt.OptionsList.GetOptionBool(option, _runner, _env, false);
                    break;

                case "IF_CONVERSION_FAILS":
                    _ifConversionFails = (IfConversionFails)_stmt.OptionsList.GetOptionLong(
                        option, _runner, _env, 1, minValue: 1, maxValue: 3);
                    break;

                default:
                    throw new Exception($"\"{option}\" is not a recognized option name.");
                }
            }
        }
        // must be run from the SQLite thread
        public static void Run(INotebook notebook, ScriptEnv env, ScriptRunner runner, Ast.ImportXlsStmt stmt)
        {
            var importer = new ImportXlsStmtRunner(notebook, env, runner, stmt);

            SqlUtil.WithTransaction(notebook, importer.Import);
        }
Example #29
0
 public App(INotebook nb)
 {
     _book = nb;
 }
Example #30
0
 public ScriptParser(INotebook notebook)
 {
     _notebook     = notebook;
     _preprocessor = new MacroProcessor(notebook);
 }