示例#1
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));
        }
示例#2
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);
        }