Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ private ScopeBindingDescriptor(ScopeBindingKind kind, IAttachableContext target,
public static ScopeBindingDescriptor Static(IAttachableContext target)
=> new ScopeBindingDescriptor(ScopeBindingKind.Static, target, -1);

public static ScopeBindingDescriptor ThisScope()
=> new ScopeBindingDescriptor(ScopeBindingKind.ThisScope, null, -1);
public static ScopeBindingDescriptor ThisScope(IAttachableContext target=null)
=> new ScopeBindingDescriptor(ScopeBindingKind.ThisScope, target, -1);

public static ScopeBindingDescriptor FrameScope(int index)
=> new ScopeBindingDescriptor(ScopeBindingKind.FrameScope, null, index);
Expand Down
20 changes: 16 additions & 4 deletions src/OneScript.Core/Compilation/CompilerFrontendBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ This Source Code Form is subject to the terms of the
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System;
using System.Collections.Generic;
using OneScript.Compilation.Binding;
using OneScript.Contexts;
using OneScript.DependencyInjection;
using OneScript.Execution;
using OneScript.Language;
using OneScript.Language.LexicalAnalysis;
using OneScript.Language.SyntaxAnalysis;
using OneScript.Language.SyntaxAnalysis.AstNodes;
using OneScript.Sources;
using System;
using System.Collections.Generic;

namespace OneScript.Compilation
{
Expand Down Expand Up @@ -64,6 +65,17 @@ public IExecutableModule Compile(SourceCode source, IBslProcess process, Type cl
return CompileInternal(symbols, parsedModule, classType, process);
}

public IExecutableModule Compile<T>(SourceCode source, IBslProcess process, T target)
where T : IAttachableContext
{
var lexer = CreatePreprocessor(source);
var symbols = PrepareSymbols(target);
var parsedModule = ParseSyntaxConstruction(lexer, source, p => p.ParseStatefulModule());

return CompileInternal(symbols, parsedModule, typeof(T), process);
}


public IExecutableModule CompileExpression(SourceCode source)
{
var lexer = new DefaultLexer
Expand Down Expand Up @@ -91,7 +103,7 @@ public IExecutableModule CompileBatch(SourceCode source)

protected abstract IExecutableModule CompileBatchInternal(SymbolTable symbols, ModuleNode parsedModule);

private SymbolTable PrepareSymbols()
private SymbolTable PrepareSymbols(IAttachableContext target = null)
{
var actualTable = new SymbolTable();
if (SharedSymbols != default)
Expand All @@ -104,7 +116,7 @@ private SymbolTable PrepareSymbols()
}

ModuleSymbols ??= new SymbolScope();
actualTable.PushScope(ModuleSymbols, ScopeBindingDescriptor.ThisScope());
actualTable.PushScope(ModuleSymbols, ScopeBindingDescriptor.ThisScope(target));

return actualTable;
}
Expand Down
15 changes: 10 additions & 5 deletions src/OneScript.Core/Compilation/ICompilerFrontend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ This Source Code Form is subject to the terms of the
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System;
using System.Collections.Generic;
using OneScript.Compilation.Binding;
using OneScript.Contexts;
using OneScript.Execution;
using OneScript.Language;
using OneScript.Sources;
using System;
using System.Collections.Generic;

namespace OneScript.Compilation
{
Expand All @@ -27,9 +28,13 @@ public interface ICompilerFrontend

IErrorSink ErrorSink { get; }

IExecutableModule Compile(SourceCode source, IBslProcess process, Type classType = null);

IExecutableModule CompileExpression(SourceCode source);
IExecutableModule Compile(SourceCode source, IBslProcess process, Type classType = null);

public IExecutableModule Compile<T>(SourceCode source, IBslProcess process, T target)
where T : IAttachableContext;


IExecutableModule CompileExpression(SourceCode source);

IExecutableModule CompileBatch(SourceCode source);
}
Expand Down
2 changes: 1 addition & 1 deletion src/OneScript.Core/Exceptions/IExceptionInfoFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace OneScript.Exceptions
/// </summary>
public interface IExceptionInfoFactory
{
BslObjectValue GetExceptionInfo(Exception exception);
BslValue GetExceptionInfo(Exception exception);

string GetExceptionDescription(IRuntimeContextInstance exceptionInfo);

Expand Down
13 changes: 6 additions & 7 deletions src/OneScript.Native/Compiler/ExpressionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ private static Expression TryFindConversionOp(Expression value, Type targetType)
// если будет ненадежно - поиграем с поиском статических конверсий
try
{
if (targetType == typeof(string))
{
return Expression.Call(value, "ToString", null, null);
}

return Expression.Convert(value, targetType);
}
catch (InvalidOperationException)
Expand Down Expand Up @@ -595,18 +600,12 @@ public static Expression CallOfInstanceMethod(Expression instance, string name,

public static Expression AccessModuleVariable(ParameterExpression thisArg, int variableIndex)
{
var contextProperty = PropertiesCache.GetOrAdd(
typeof(NativeClassInstanceWrapper),
nameof(NativeClassInstanceWrapper.Context),
BindingFlags.Instance | BindingFlags.Public);

var contextAccess = Expression.Property(thisArg, contextProperty);
var getVariableMethod = OperationsCache.GetOrAdd(
typeof(IAttachableContext),
nameof(IAttachableContext.GetVariable),
BindingFlags.Instance | BindingFlags.Public);

var iVariable = Expression.Call(contextAccess, getVariableMethod, Expression.Constant(variableIndex));
var iVariable = Expression.Call(thisArg, getVariableMethod, Expression.Constant(variableIndex));
var valueProperty = PropertiesCache.GetOrAdd(
typeof(IValueReference),
nameof(IValueReference.BslValue),
Expand Down
Loading