Skip to content

Commit

Permalink
v0.11.0に
Browse files Browse the repository at this point in the history
  • Loading branch information
taiseiue committed Nov 6, 2024
1 parent 513b7e3 commit b7d94ef
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 13 deletions.
9 changes: 9 additions & 0 deletions Losetta.Runtime/Core/Alice.Core.Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ public static Variable Ref([BindInfo] ParsingScript script)
Parser.NeedReferenceNext = true;
return Utils.GetItem(script);
}
[AliceFunction(Attribute = FunctionAttribute.LANGUAGE_STRUCTURE, Context = ParsingScript.Contexts.IN_ARGS)]
public static Variable Out([BindInfo] ParsingScript script)
{
Parser.NeedReferenceNext = true;
var vf = Utils.GetItem(script).Reference as ValueFunction;
string name = vf.Name;
script.ParentScript.Variables[name] = vf;
return new Variable(vf);
}
[AliceFunction(Attribute = FunctionAttribute.LANGUAGE_STRUCTURE, Name = "__makeref")]
public static Variable MakeRef([BindInfo] ParsingScript script) => Ref(script);
[AliceFunction(Attribute = FunctionAttribute.LANGUAGE_STRUCTURE, Name = "__useref")]
Expand Down
2 changes: 1 addition & 1 deletion Losetta.Runtime/Losetta.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<ApplicationIcon>alice_logo.ico</ApplicationIcon>
<Version>0.10.5</Version>
<Version>0.11.0</Version>
<Company>WSOFT</Company>
<Product>Losetta</Product>
<Title>Alice.Runtime</Title>
Expand Down
9 changes: 9 additions & 0 deletions Losetta.Tests/Losetta/Scripting/Statement/Others.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ number add(number x, number y)

Assert.That(TestUtils.Script.Execute<int>(code), Is.EqualTo(5));
}
[TestCase]
public void Dictionary()
{
string code = @"
dictionary dic = {""key1"": 1, ""key2"": 2};
return dic;
";
Assert.That(TestUtils.Script.Execute<Dictionary<string,int>>(code)["key1"], Is.EqualTo(1));
}
}
4 changes: 4 additions & 0 deletions Losetta/Alice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public static Variable Execute(string code, string filename = "", bool mainFile
{
return Interpreter.Instance.Process(code, filename, mainFile);
}
public static Variable Execute(string code, (string, object)[] variables, string filename = "", bool mainFile = false)
{
return Interpreter.Instance.Process(code, filename, mainFile, variables: variables);
}
/// <summary>
/// このインタプリタで読み込み可能なファイルを読み込みます
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion Losetta/Functions/AssignFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public static Variable Assign(ParsingScript script, string varName, bool localIf
{
string m_name = Constants.GetRealName(varName);
script.CurrentAssign = m_name;
Variable varValue = Utils.GetItem(script);
script.MoveForwardIf(Constants.ASSIGNMENT.ToCharArray());
Variable varValue = script.Prev == Constants.ASSIGNMENT[0] ? Utils.GetItem(script) : varType.Activate(new List<Variable>(), script);
bool registVar = varType is not null;
if (varType is null)
{
Expand Down
11 changes: 5 additions & 6 deletions Losetta/Functions/ParserFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,11 @@ private bool CheckValidFunction(ref ParserFunction func, ParsingScript script,Ha
func = new ValueFunction();
return true;
}
else if(script.Current == Constants.ASSIGNMENT[0])
{
var value = AssignFunction.Assign(script, name, false, null, keywords, type);
func = new ValueFunction(value);
return true;
}

var value = AssignFunction.Assign(script, name, false, null, keywords, type);
func = new ValueFunction(value);
return true;

}
if(func is not null && (func is not FunctionBase fb || fb.Context.HasFlag(script.Context)))
{
Expand Down
10 changes: 9 additions & 1 deletion Losetta/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public ParsingScript GetScript(string script, string filename = "", bool mainFil
}
return toParse;
}
public Variable Process(string script, string filename = "", bool mainFile = false, object tag = null, AlicePackage package = null)
public Variable Process(string script, string filename = "", bool mainFile = false, object tag = null, AlicePackage package = null, (string, object)[] variables = null)
{
string data = PreProcessor.ConvertToScript(script, out Dictionary<int, int> char2Line, out var def, out var setting, filename);
if (string.IsNullOrWhiteSpace(data))
Expand All @@ -295,6 +295,14 @@ public Variable Process(string script, string filename = "", bool mainFile = fal
toParse.Tag = tag;
toParse.Package = package;

if(variables is not null)
{
foreach (var (name, value) in variables)
{
toParse.Variables[name] = new ValueFunction(new Variable(value));
}
}

if (mainFile)
{
toParse.MainFilename = toParse.Filename;
Expand Down
2 changes: 1 addition & 1 deletion Losetta/Losetta.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<ApplicationIcon>alice_logo.ico</ApplicationIcon>
<Version>0.10.5</Version>
<Version>0.11.0</Version>
<Company>WSOFT</Company>
<Product>Losetta</Product>
<Title>Alice.Runtime</Title>
Expand Down
11 changes: 11 additions & 0 deletions Losetta/Objects/Delegate.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AliceScript.Functions;
using AliceScript.Parsing;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace AliceScript.Objects
Expand Down Expand Up @@ -113,6 +114,16 @@ public Variable Invoke(Variable arg = null, ParsingScript script = null, AliceSc
}
return Invoke(args, script, instance);
}
public Variable Invoke(object[] args, ParsingScript script = null, AliceScriptClass.ClassInstance instance = null)
{
var vars = args.Select(arg => new Variable(arg)).ToList();
return Invoke(vars, script, instance);
}
public Variable Invoke(params object[] args)
{
var vars = args.Select(arg => new Variable(arg)).ToList();
return Invoke(args, ParsingScript.GetTopLevelScript(), null);
}
public void BeginInvoke(List<Variable> args = null, ParsingScript script = null, AliceScriptClass.ClassInstance instance = null)
{
m_BeginInvokeMessanger mb = new m_BeginInvokeMessanger();
Expand Down
19 changes: 18 additions & 1 deletion Losetta/Objects/TypeObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ public Variable Activate(List<Variable> args, ParsingScript script)
{
if (ClassType is not null)
{
//TODO:非ObjectBaseのクラスのアクティベート
if(ClassType is BindObject bind)
{
return new Variable(bind.Constructor.Evaluate(args, script));
}
if (ClassType is ObjectBase csClass)
{
return csClass.GetImplementation(args, script);
Expand All @@ -166,6 +169,20 @@ public Variable Activate(List<Variable> args, ParsingScript script)
v.Tuple.Type = ArrayType;
return v;
}
if(!Nullable)
{
switch(Type)
{
case Variable.VarType.BOOLEAN:
return new Variable(args.Count > 0 ? args[0].AsBool() : false);
case Variable.VarType.NUMBER:
return new Variable(args.Count > 0 ? args[0].AsDouble() : 0);
case Variable.VarType.STRING:
return new Variable(args.Count > 0 ? args[0].AsString() : "");
case Variable.VarType.VOID:
return Variable.EmptyInstance;
}
}
return new Variable(Type);
}

Expand Down
22 changes: 21 additions & 1 deletion Losetta/Variable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ public T ConvertTo<T>()
/// <exception cref="ScriptException">型の不一致により変換できない場合にスローされる例外</exception>
public object ConvertTo(Type type)
{
return TryConvertTo(type, out object o) ? o : throw new ScriptException("型が一致しないか、変換できません。", Exceptions.WRONG_TYPE_VARIABLE);
return TryConvertTo(type, out object o) ? o : throw new ScriptException($"`{this.AsType()}`を`{type}`にキャストできません。", Exceptions.WRONG_TYPE_VARIABLE);
}
/// <summary>
/// この変数を指定した型に変換できるか試みます
Expand Down Expand Up @@ -1094,6 +1094,26 @@ public bool TryConvertTo(Type type, out object result)
result = m_dictionary;
return true;
}
if(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>))
{
var keyType = type.GetGenericArguments()[0];
var valueType = type.GetGenericArguments()[1];
var dict = (IDictionary)Activator.CreateInstance(type);
foreach (var kv in m_dictionary)
{
if (kv.Key.TryConvertTo(keyType, out var k) && kv.Value.TryConvertTo(valueType, out var v))
{
dict.Add(k, v);
}
else
{
result = null;
return false;
}
}
result = dict;
return true;
}
if (type is null || type == typeof(VariableCollection))
{
result = new VariableCollection(m_dictionary.Select(item => new Variable(item)).ToList());
Expand Down
2 changes: 1 addition & 1 deletion alice/alice.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<ApplicationIcon>alice_logo.ico</ApplicationIcon>
<Version>0.10.5</Version>
<Version>0.11.0</Version>
<Title>Losetta.CLI</Title>
<Authors>WSOFT</Authors>
<Company>WSOFT</Company>
Expand Down

0 comments on commit b7d94ef

Please sign in to comment.