Skip to content

Commit

Permalink
Merge pull request #115 from WSOFT-Project/develop
Browse files Browse the repository at this point in the history
Losetta v0.10.2
  • Loading branch information
taiseiue authored Dec 23, 2023
2 parents 4bbba54 + 58254e0 commit 3e46ca2
Show file tree
Hide file tree
Showing 23 changed files with 1,054 additions and 941 deletions.
236 changes: 184 additions & 52 deletions Losetta.Runtime/Alice.IO.cs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Losetta.Runtime/Alice.Security.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public static void Init()
internal sealed class SecurityFunctions
{
#region バイト配列暗号化
public static byte[] Data_Encrypt(byte[] data, string password)
public static byte[] Data_Encrypt(byte[] data, string password, int keySize = 128, int iterations = 1024, bool useSHA512 = false)
{
return FileEncrypter.Encrypt(data, password);
return FileEncrypter.Encrypt(data, password, keySize, iterations, useSHA512);
}
public static byte[] Data_Decrypt(byte[] data, string password)
public static byte[] Data_Decrypt(byte[] data, string password, int keySize = 128, int iterations = 1024, bool useSHA512 = false)
{
return FileEncrypter.Decrypt(data, password);
return FileEncrypter.Decrypt(data, password, keySize, iterations, useSHA512);
}
#endregion

Expand Down
10 changes: 2 additions & 8 deletions Losetta.Runtime/Core/Alice.Core.Flow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,11 @@ Variable ProcessEach(Variable current)
Variable result = ProcessEach(new Variable(enumerator.Current));
if (result.IsReturn || result.Type == Variable.VarType.BREAK)
{
if (disposable is not null)
{
disposable.Dispose();
}
disposable?.Dispose();
return result;
}
}
if (disposable is not null)
{
disposable.Dispose();
}
disposable?.Dispose();
break;
}
}
Expand Down
1 change: 1 addition & 0 deletions Losetta.Runtime/Core/Alice.Core.Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static void SpinWait(int iterations)
{
Thread.SpinWait(iterations);
}
[AliceFunction(Attribute = FunctionAttribute.FUNCT_WITH_SPACE)]
public static void Exit()
{
Alice.OnExiting();
Expand Down
44 changes: 44 additions & 0 deletions Losetta.Runtime/Core/Extension/Alice.Core.Bytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using AliceScript.Binding;
using AliceScript.Functions;
using System.Text;

namespace AliceScript.NameSpaces.Core
{
internal partial class CoreFunctions
{
public static string ToString(this byte[] data, string charCode)
{
return Encoding.GetEncoding(charCode).GetString(data);
}
public static string ToString(this byte[] data, int codePage)
{
return Encoding.GetEncoding(codePage).GetString(data);
}
public static string ToBase64(this byte[] data)
{
return System.Convert.ToBase64String(data);
}
public static string ToBase64(this byte[] data, int offcset, int length)
{
return System.Convert.ToBase64String(data, offcset, length);
}
public static string ToBase64(this byte[] data, int offcset, int length, bool insertLineBrakes)
{
return System.Convert.ToBase64String(data, offcset, length, insertLineBrakes ? Base64FormattingOptions.InsertLineBreaks : Base64FormattingOptions.None);
}
public static string ToBase64(this byte[] data, bool insertLineBrakes)
{
return System.Convert.ToBase64String(data, 0, data.Length, insertLineBrakes ? Base64FormattingOptions.InsertLineBreaks : Base64FormattingOptions.None);
}
[AliceFunction(Attribute = FunctionAttribute.LANGUAGE_STRUCTURE)]
public static int Length(this byte[] data)
{
return data.Length;
}
[AliceFunction(Attribute = FunctionAttribute.LANGUAGE_STRUCTURE)]
public static int Size(this byte[] data)
{
return data.Length;
}
}
}
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>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<ApplicationIcon>alice_logo.ico</ApplicationIcon>
<Version>0.10.1</Version>
<Version>0.10.2</Version>
<Company>WSOFT</Company>
<Product>Losetta</Product>
<Title>Alice.Runtime</Title>
Expand Down
8 changes: 4 additions & 4 deletions Losetta/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,17 @@ public static partial class Constants
/// <summary>
/// UTF16表現がとるパターン
/// </summary>
public static readonly Regex UTF16_LITERAL = new Regex(@"\\u[0-9a-fA-F]{4}", RegexOptions.Compiled);
public static readonly Regex UTF16_LITERAL = new Regex(@"[^\\]\\u[0-9a-fA-F]{4}", RegexOptions.Compiled);

/// <summary>
/// 可変長UTF16表現がとるパターン
/// </summary>
public static readonly Regex UTF16_VARIABLE_LITERAL = new Regex(@"\\x[0-9a-fA-F]{1,4}", RegexOptions.Compiled);
public static readonly Regex UTF16_VARIABLE_LITERAL = new Regex(@"[^\\]\\x[0-9a-fA-F]{1,4}", RegexOptions.Compiled);

/// <summary>
/// UTF32表現がとるパターン
/// </summary>
public static readonly Regex UTF32_LITERAL = new Regex(@"\\U[0-9a-fA-F]{8}", RegexOptions.Compiled);
public static readonly Regex UTF32_LITERAL = new Regex(@"[^\\]\\U[0-9a-fA-F]{8}", RegexOptions.Compiled);

/// <summary>
/// インデックスの逆引きがとるパターン
Expand All @@ -243,7 +243,7 @@ public static partial class Constants
public const string OVERRIDE = "override";
public const string COMMAND = "command";
public const string READONLY = "readonly";
public const string EXTENSION = "extension";
public const string EXTENSION = "extonly";
public const string FUNCTION = "function";


Expand Down
18 changes: 11 additions & 7 deletions Losetta/Extra/SafeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ public static class SafeReader
/// ReadJEncを使用して、ファイルから文字列を取得します。
/// </summary>
/// <param name="filename">取得するファイル名</param>
/// <param name="charcode">判定された文字コード</param>
/// <param name="charCode">判定された文字コード</param>
/// <param name="codePage">判定された文字コードの番号</param>
/// <returns>ファイル内の文字列</returns>
/// <exception cref="FileNotFoundException">指定されたファイルが存在しません</exception>
public static string ReadAllText(string filename, out string charcode)
public static string ReadAllText(string filename, out string charCode, out int codePage)
{
if (string.IsNullOrEmpty(filename)) { charcode = ""; return string.Empty; }
if (string.IsNullOrEmpty(filename)) { charCode = ""; codePage = -1; return string.Empty; }
FileInfo file = new FileInfo(filename);
if (!file.Exists)
{
Expand All @@ -25,20 +26,23 @@ public static string ReadAllText(string filename, out string charcode)

using FileReader reader = new FileReader(file);
CharCode c = reader.Read(file);
charcode = c.Name;
codePage = c.CodePage;
charCode = c.Name;
return reader.Text;
}
/// <summary>
/// ReadJEncを使用して、バイト配列から文字列を取得します
/// </summary>
/// <param name="data">取得するデータ</param>
/// <param name="charcode">判定された文字コード</param>
/// <param name="charCode">判定された文字コード</param>
/// <param name="codePage">判定された文字コードの番号</param>
/// <returns>データ内の文字列</returns>
public static string ReadAllText(byte[] data, out string charcode)
public static string ReadAllText(byte[] data, out string charCode, out int codePage)
{
using FileReader reader = new FileReader(data);
CharCode c = reader.Read(data);
charcode = c.Name;
codePage = c.CodePage;
charCode = c.Name;
return reader.Text;
}
}
Expand Down
96 changes: 26 additions & 70 deletions Losetta/Functions/AssignFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ public Variable Assign(ParsingScript script, string varName, bool localIfPossibl
}

bool registVar = type_modifer is not null;
bool registConst = Keywords.Contains(Constants.CONST);
AccessModifier accessModifier = Keywords.Contains(Constants.PUBLIC) ? AccessModifier.PUBLIC : AccessModifier.PRIVATE;
accessModifier = Keywords.Contains(Constants.PRIVATE) ? AccessModifier.PUBLIC : accessModifier;
accessModifier = Keywords.Contains(Constants.PROTECTED) ? AccessModifier.PROTECTED : accessModifier;
bool isReadOnly = Keywords.Contains(Constants.READONLY);

if (Keywords.Contains(Constants.CONST))
{
registVar = true;
isReadOnly = true;
}

script.MoveBackIfPrevious(Constants.END_ARG);
varValue.TrySetAsMap();

Expand All @@ -58,83 +63,34 @@ public Variable Assign(ParsingScript script, string varName, bool localIfPossibl
Utils.ThrowErrorMsg("[" + script.Rest + "]は無効なトークンです", Exceptions.INVALID_TOKEN,
script, m_name);
}
if (registConst)
// First try processing as an object (with a dot notation):
Variable result = ProcessObject(m_name, script, varValue);
if (result is not null)
{
Utils.CheckLegalName(m_name);
//定数定義
if (!FunctionExists(m_name, script, out var func))
{
// Check if the variable to be set has the form of x[a][b]...,
// meaning that this is an array element.
List<Variable> arrayIndices = Utils.GetArrayIndices(script, m_name, (name) => { m_name = name; }, this);
m_name = Constants.ConvertName(m_name);
if (arrayIndices.Count == 0)
{
if (isGlobal)
{
ParsingScript.GetTopLevelScript(baseScript).Consts.Add(m_name, new ValueFunction(varValue));
}
else
{
baseScript.Consts.Add(m_name, new ValueFunction(varValue));
}
Variable retVar = varValue.DeepClone();
retVar.CurrentAssign = m_name;
return retVar;
}

Variable array;

ParserFunction pf = ParserFunction.GetVariable(m_name, script, false, Keywords);
array = pf is not null ? pf.GetValue(script) : new Variable();

ExtendArray(array, arrayIndices, 0, varValue);
if (isGlobal)
{
ParsingScript.GetTopLevelScript(baseScript).Consts.Add(m_name, new ValueFunction(varValue));
}
else
{
baseScript.Consts.Add(m_name, new ValueFunction(varValue));
}
return array;
}
else
{
throw new ScriptException("定数に値を代入することはできません", Exceptions.CANT_ASSIGN_TO_READ_ONLY, script);
}
return result;
}
else
{
// First try processing as an object (with a dot notation):
Variable result = ProcessObject(m_name, script, varValue);
if (result is not null)
{
return result;
}

// 設定する変数が x[a][b]... のような形式かどうかをチェックする
// つまり、配列添え字演算子が書いてあるかどうかを確認
List<Variable> arrayIndices = Utils.GetArrayIndices(script, m_name, (name) => { m_name = name; }, this);
// 設定する変数が x[a][b]... のような形式かどうかをチェックする
// つまり、配列添え字演算子が書いてあるかどうかを確認
List<Variable> arrayIndices = Utils.GetArrayIndices(script, m_name, (name) => { m_name = name; }, this);

if (arrayIndices.Count == 0)
{
ParserFunction.AddGlobalOrLocalVariable(m_name, new ValueFunction(varValue), baseScript, localIfPossible, registVar, accessModifier, type_modifer, isReadOnly, true);
Variable retVar = varValue.DeepClone();
retVar.CurrentAssign = m_name;
return retVar;
}
if (arrayIndices.Count == 0)
{
ParserFunction.AddGlobalOrLocalVariable(m_name, new ValueFunction(varValue), baseScript, localIfPossible, registVar, accessModifier, type_modifer, isReadOnly, true);
Variable retVar = varValue.DeepClone();
retVar.CurrentAssign = m_name;
return retVar;
}

Variable array;
Variable array;

ParserFunction pf = ParserFunction.GetVariable(m_name, baseScript);
array = pf is not null ? pf.GetValue(script) : new Variable();
ParserFunction pf = ParserFunction.GetVariable(m_name, baseScript);
array = pf is not null ? pf.GetValue(script) : new Variable();

ExtendArray(array, arrayIndices, 0, varValue);
ExtendArray(array, arrayIndices, 0, varValue);

ParserFunction.AddGlobalOrLocalVariable(m_name, new ValueFunction(array), baseScript, localIfPossible, registVar, accessModifier, type_modifer, isReadOnly, true);
return array;
}
ParserFunction.AddGlobalOrLocalVariable(m_name, new ValueFunction(array), baseScript, localIfPossible, registVar, accessModifier, type_modifer, isReadOnly, true);
return array;
}
internal static Variable ProcessObject(string m_name, ParsingScript script, Variable varValue)
{
Expand Down
47 changes: 4 additions & 43 deletions Losetta/Functions/CustomFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public CustomFunction(string funcName,
if (parms)
{
parmsindex = i;
argName = argName.Substring(Constants.PARAMS.Length);
//argName = argName.Substring(Constants.PARAMS.Length);
argName = argName.Trim();
}
if (parms && refs)
Expand Down Expand Up @@ -312,6 +312,7 @@ private void RegisterArguments(List<Variable> args,
arg.Name = m_args[i];
//m_VarMap[m_args[i]] = arg;
script.Variables[m_args[i]] = arg;
i = maxSize;
}
else
{
Expand Down Expand Up @@ -339,49 +340,10 @@ private void RegisterArguments(List<Variable> args,
script.Variables[m_args[i]] = arg;
}
}
for (int i = m_args.Length; i < args.Count; i++)
if (parmsindex < 0 && m_args.Length < args.Count)
{
Variable val;

bool refd = args[i].Keywords.Contains(Constants.REF);
if (m_refMap.Contains(i))
{
val = refd
? args[i]
: throw new ScriptException("引数 `" + i + "` は `" + Constants.REF + "` キーワードと共に渡さなければなりません。", Exceptions.ARGUMENT_MUST_BE_PASSED_WITH_KEYWORD, script);
}
else
{
if (refd)
{
throw new ScriptException("引数 `" + i + "` は `" + Constants.REF + "' キーワードと共に使用することができません。", Exceptions.ARGUMENT_CANT_USE_WITH_KEYWORD, script);
}
val = new Variable();
val.Assign(args[i]);
}
var arg = new ValueFunction(val);
if (i + 1 > m_args.Length)
{
throw new ScriptException($"関数 `{m_name}`は、{m_args.Length}個よりも多く引数を持つことができません", Exceptions.TOO_MANY_ARGUREMENTS, script);
}
arg.Name = m_args[i];
script.Variables[args[i].ParamName] = arg;
}

if (NamespaceData is not null)
{
var vars = NamespaceData.Variables;
string prefix = NamespaceData.Name + ".";
foreach (KeyValuePair<string, ParserFunction> elem in vars)
{
string key = elem.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) ?
elem.Key.Substring(prefix.Length) : elem.Key;

//m_VarMap[key] = elem.Value;
script.Variables[key] = elem.Value;
}
throw new ScriptException($"関数 `{m_name}`は、{m_args.Length}個よりも多く引数を持つことができません", Exceptions.TOO_MANY_ARGUREMENTS, script);
}

}
public Variable GetVariable(ParsingScript script, Variable current)
{
Expand Down Expand Up @@ -459,7 +421,6 @@ public override ParserFunction NewInstance()
/// </summary>
public int ArgumentCount => m_args.Length;

public StackLevel NamespaceData { get; set; }
public TypeObject MethodRequestType => IsMethod && m_typArgMap.Count >= m_this ? m_typArgMap[m_this] : new TypeObject();

public int DefaultArgsCount => m_defaultArgs.Count;
Expand Down
2 changes: 1 addition & 1 deletion Losetta/Functions/FunctionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private List<Variable> GetFunctionArguments(ParsingScript script)
return args;
}

private FunctionBaseEventArgs InitializeFunctionEventArgs(ParsingScript script, Variable currentVariable, List<Variable> args)
private static FunctionBaseEventArgs InitializeFunctionEventArgs(ParsingScript script, Variable currentVariable, List<Variable> args)
{
FunctionBaseEventArgs functionEventArgs = new FunctionBaseEventArgs
{
Expand Down
Loading

0 comments on commit 3e46ca2

Please sign in to comment.