internal static InitializerGenerator GetInitializerGenerator(NativeCompiler compiler, CompiledInitializer initializer)
        {
            if (initializer == null)
            {
                throw new ArgumentNullException("initializer");
            }
            switch (initializer.Type)
            {
            case InitializerType.ProgramInitializer:
                RuntimeProgramInitializer programInitializer = initializer as RuntimeProgramInitializer;
                if (programInitializer == null)
                {
                    throw new Exception("Expected to see a RuntimeProgramInitializer");     // ... since we have compiled everything ourself.
                }
                return(new ProgramInitializerGenerator(compiler, programInitializer));

            case InitializerType.GlobalInitializer:
                RuntimeGlobalInitializer globalInitializer = initializer as RuntimeGlobalInitializer;
                if (globalInitializer == null)
                {
                    throw new Exception("Expected to see a RuntimeGlobalInitializer");     // ... since we have compiled everything ourself.
                }
                return(new GlobalInitializerGenerator(compiler, globalInitializer));

            case InitializerType.ClassInitializer:
                RuntimeGlobalInitializer classInitializer = initializer as RuntimeGlobalInitializer;
                if (classInitializer == null)
                {
                    throw new Exception("Expected to see a RuntimeGlobalInitializer");     // ... since we have compiled everything ourself.
                }
                return(new ClassInitializerGenerator(compiler, classInitializer));

            case InitializerType.PoolVariableInitializer:
                RuntimePoolItemInitializer poolItemInitializer = initializer as RuntimePoolItemInitializer;
                if (poolItemInitializer == null)
                {
                    throw new Exception("Expected to see a RuntimePoolItemInitializer");     // ... since we have compiled everything ourself.
                }
                return(new PoolVariableInitializerGenerator(compiler, poolItemInitializer));

            default:
                throw new Exception(String.Format("Unrecognized initializer type {0}", initializer.Type));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Parses the source code within a specified compiler context.
        /// The source unit to parse is held on by the context.
        /// </summary>
        /// <returns><b>null</b> on failure.</returns>
        /// <remarks>Could also set the code properties and line/file mappings on the source unit.</remarks>
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            if (sourceUnit == null)
            {
                throw new ArgumentNullException("sourceUnit");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            if (errorSink == null)
            {
                throw new ArgumentNullException("errorSink");
            }
            if (sourceUnit.LanguageContext != this)
            {
                throw new ArgumentException("Language context mismatch");
            }

            // Ensure that sources are installed into the image.
            this.EnsureInitialized();


            // THE CODE BELOW NEEDS CLEAN-UP !!!
            // 1. Parse the code to an AST
            Parser parser = new Parser();

            parser.ErrorSink = new ErrorSinkWrapper(sourceUnit, errorSink);
            InitializerNode node = parser.ParseInitializer(sourceUnit.GetReader());

            if ((node == null) || !node.Accept(ParseTreeValidatingVisitor.Current))
            {
                return(null); // Failed to compile the code, return null
            }
            // 2. Compile the AST to RuntimeProgramInitializer
            RuntimeProgramInitializer code = new RuntimeProgramInitializer(node, null);

            if (code.Validate(this.SmalltalkEnvironment.Runtime.GlobalScope, new ErrorSinkWrapper(sourceUnit, errorSink)))
            {
                return(null); // Failed to compile the code, return null
            }
            // 3. Create a script-source that wraps the Lambda Expression and compiles it to a delegate
            return(new SmalltalkScriptCode(code, this.SmalltalkEnvironment.Runtime, sourceUnit));
        }
Esempio n. 3
0
        private Expression <Func <object, ExecutionContext, object> > JitExpression()
        {
            if (this.Environment == null)
            {
                MessageBox.Show("First, create the environment.");
                return(null);
            }

            Properties.Settings.Default.LastWorkspaceInstallSource = this.textInstall.Text;
            Properties.Settings.Default.LastWorkspaceEvalSource    = this.textEvaluate.Text;
            Properties.Settings.Default.Save();

            this.textResultEvaluate.Text = null;

            string txt = this.textEvaluate.SelectedText;

            if (String.IsNullOrEmpty(txt))
            {
                txt = this.textEvaluate.Text;
            }
            StringReader reader = new StringReader(txt);

            ErrorSink errorSink = new ErrorSink(this.textResultEvaluate);
            Parser    parser    = new Parser();

            parser.ErrorSink = errorSink;
            InitializerNode node = parser.ParseInitializer(reader);

            if (errorSink.HadErrors)
            {
                return(null);
            }

            RuntimeProgramInitializer code = new RuntimeProgramInitializer(node, null);
            var compilationResult          = code.Compile(this.Environment.Runtime);

            if (compilationResult == null)
            {
                return(null);
            }
            return(compilationResult);
        }
Esempio n. 4
0
        public bool Evaluate()
        {
            string       txt    = this.Client.EvaluateSourceCode;
            StringReader reader = new StringReader(txt);

            ErrorSink errorSink = new ErrorSink(this.Client);
            Parser    parser    = new Parser();

            parser.ErrorSink = errorSink;
            InitializerNode node = parser.ParseInitializer(reader);

            if (errorSink.HadErrors)
            {
                return(false);
            }

            try
            {
                RuntimeProgramInitializer code = new RuntimeProgramInitializer(node, null);
                if (!code.Validate(this.Environment.Runtime.GlobalScope, errorSink))
                {
                    return(false);
                }
                this.LastResult = code.Execute(null, new ExecutionContext(this.Environment.Runtime));
            }
            catch (IronSmalltalk.Runtime.Internal.SmalltalkDefinitionException ex)
            {
                if (this.Client != null)
                {
                    this.Client.ReportError(ex.Message, SourceLocation.Invalid, SourceLocation.Invalid);
                }
                return(false);
            }
            catch (IronSmalltalk.Runtime.Internal.SmalltalkRuntimeException ex)
            {
                if (this.Client != null)
                {
                    this.Client.ReportError(ex.Message, SourceLocation.Invalid, SourceLocation.Invalid);
                }
                return(false);
            }
            catch (Exception ex)
            {
                if (this.Client != null)
                {
                    this.Client.ReportError(ex.Message, SourceLocation.Invalid, SourceLocation.Invalid);
                }
                return(false);
            }
            this.PrintResult(this.LastResult);


            //dynamic rt = this.Environment.Runtime;
            //string x = rt.GetTestString();
            //dynamic x = this.LastResult;
            //dynamic y = x.PrintString();
            //y = x.PrintString;
            //int z = x.Hash();

            return(true);
        }