コミットを比較
| 作成者 | SHA1 | 日付 | |
|---|---|---|---|
|
|
381a717942
|
検索条件に一致するファイルはありません
+39
-53
@@ -1,75 +1,61 @@
|
|||||||
# v1.16.0 - Lua スクリプト for YMM4
|
# v1.10.0 - Lua スクリプト for YMM4
|
||||||
|
|
||||||
標準の Lua には無い簡易な記法を構文拡張へ 6 種類追加したリリースです。三項演算子、nil 条件演算子、ラムダ式、パイプ、補間文字列、範囲判定を書けるようになりました。いずれもスクリプトの実行前に等価な標準 Lua へ変換するため、LuaJIT、MoonSharp、CPU カーネルのどの実行エンジンでも同じ結果になります。変換は字句単位で行い、文字列とコメントの内側は変換しません。入口とする記号は通常の Lua には現れないため、既存のスクリプトの書き方と実行結果に変更はありません。
|
エフェクトパネルへ新しいパラメータ入力欄を追加したリリースです。整数スライダー 4 本と、
|
||||||
|
テキスト・フォント・フォルダ・ファイルの文字列入力をスクリプトから参照できるようにしました。
|
||||||
|
文字列パラメータは MoonSharp とネイティブの両エンジンで同じ名前から参照でき、ネイティブでは
|
||||||
|
共有メモリの文字列領域を必要量に応じて拡張・縮小します。あわせて毎フレームのアロケーションを
|
||||||
|
見直し、補完候補を拡充しました。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 新機能
|
## 新機能・機能改善
|
||||||
|
|
||||||
### 三項演算子
|
### 1. 整数スライダー obj.slider0〜3
|
||||||
|
|
||||||
条件 ? A : B を追加しました。条件が真のとき A を、偽のとき B を返します。条件と両辺は同じ行に式として書きます。
|
数値スライダーを 4 本追加しました。各値は `obj.slider0`〜`obj.slider3` として参照できます。
|
||||||
|
|
||||||
- (function() if 条件 then return (A) else return (B) end end)() へ展開します。条件は 1 度だけ評価し、選ばれた側の式だけを評価します。
|
- 値は整数で、小数は扱いません。表示範囲は `obj.slider0` が 1〜10、`obj.slider1` が 0〜100、
|
||||||
- a and b or c と違い、真の側の値が false や nil であっても、その値をそのまま返します。
|
`obj.slider2` が 0〜1000、`obj.slider3` が -1000〜1000 です。
|
||||||
- : のあとにもう一度 ? : を書けば、else 側で分岐を続けられます。
|
- 状態の切り替えなど、補間を伴わない整数値の受け渡しに使えます。
|
||||||
- 関数呼び出しの引数の位置にも書けます。? を値の位置に書いたときだけ変換するため、テーブルリテラルやメソッド呼び出し、ラベルなど、通常のコードには影響しません。
|
- MoonSharp・ネイティブ・CPU/GPU カーネルのいずれのレーンからも参照できます。
|
||||||
|
|
||||||
### nil 条件演算子
|
### 2. 文字列パラメータ obj.text / obj.font / obj.dir / obj.file_*
|
||||||
|
|
||||||
t?.x と t?[k] を追加しました。t が nil のときは nil を返し、そうでなければ t.x や t[k] を返します。nil の値へ添字アクセスして実行時エラーになるのを防ぎます。
|
テキスト・フォント・フォルダ・ファイルの入力欄を追加しました。
|
||||||
|
|
||||||
- 対象を一時変数へ受けて 1 度だけ評価する無名関数へ展開します。(function() local tmp = (t) if tmp == nil then return nil end return tmp.x end)() の形になります。
|
- `obj.text` はテキスト入力欄の文字列、`obj.font` はフォント名、`obj.dir` はフォルダのパスです。
|
||||||
- a?.b?.c のように連ねれば、途中の値が nil でも安全にたどれます。
|
- ファイル選択は種類ごとに `obj.file_video` / `obj.file_audio` / `obj.file_image` /
|
||||||
- nil 合体演算子と組み合わせて、t?.x ?? 既定値 のように書けます。左辺が nil のときだけ既定値を返します。
|
`obj.file_project` / `obj.file_mp4` / `obj.file_exo` / `obj.file_subtitle` /
|
||||||
|
`obj.file_shader` で参照し、選択したファイルのパスを取得します。
|
||||||
|
- 取得したパスは `obj.load` へ渡して画像・動画の読み込みに使えます。
|
||||||
|
- MoonSharp とネイティブの両エンジンで同じ名前から参照できます。
|
||||||
|
|
||||||
### ラムダ式
|
### 3. 補完候補の拡充
|
||||||
|
|
||||||
(a, b) => 式 を追加しました。その式を返す無名関数へ展開し、(function(a, b) return 式 end) になります。
|
スクリプトエディタの補完候補へ `obj.slider0`〜`obj.slider3` と、追加した文字列パラメータの
|
||||||
|
各名前を加えました。
|
||||||
- 引数が 1 つのときは括弧を省いて x => 式、引数が無いときは () => 式 と書けます。最後の引数として ... を受け取れます。本体には 1 つの式を書きます。
|
|
||||||
- table.sort の比較関数のように、関数を引数へ直接渡す場面で使えます。
|
|
||||||
- 入口は => だけのため、比較演算子の >= や <= には影響しません。
|
|
||||||
|
|
||||||
### パイプ
|
|
||||||
|
|
||||||
x |> f を追加しました。f((x)) へ展開し、左辺を関数の第 1 引数として渡します。
|
|
||||||
|
|
||||||
- x |> f(a, b) のように引数を書いたときは、左辺が第 1 引数に入り、残りの引数は後ろへ続いて f((x), a, b) になります。
|
|
||||||
- |> は左から右へ連ねられます。a |> f |> g は g(f(a)) と同じ順序で評価します。
|
|
||||||
- math.floor のようなメンバー関数も呼べます。
|
|
||||||
|
|
||||||
### 補間文字列
|
|
||||||
|
|
||||||
バッククォートで囲む文字列を追加しました。中に {式} を書くと、その値を tostring で文字列にして差し込みます。
|
|
||||||
|
|
||||||
- `x={a}` は ("x=" .. tostring(a)) へ展開します。式を含まない `hello` は "hello" に、空の `` は "" になります。
|
|
||||||
- { } の中では拡張構文もそのまま使えます。バックスラッシュによるエスケープは保持します。
|
|
||||||
- 入口はバッククォートのため、通常の文字列やコメントの中のバッククォートは変換しません。
|
|
||||||
|
|
||||||
### 範囲判定
|
|
||||||
|
|
||||||
x in <1..5> を追加しました。x がその範囲に入っているかどうかを判定します。.. は終端を含み、..< は終端を含みません。
|
|
||||||
|
|
||||||
- 左辺を一時変数へ受けて 1 度だけ評価する無名関数へ展開し、1 <= x and x <= 5 に相当する判定を返します。<1..<5> は上限を含まず、1 <= x and x < 5 になります。
|
|
||||||
- 範囲 for の for i in <0..10> とは書く位置が異なるため、for のヘッダーや、ipairs、pairs を使う汎用 for には影響しません。
|
|
||||||
- 増分を伴う <開始..終了, 増分> の書き方は、範囲判定では扱いません。
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 互換性・後方互換
|
## 互換性・後方互換
|
||||||
|
|
||||||
- 追加した記法はいずれも、標準の Lua には現れない記号、つまり ? と => と |> とバッククォートと in < の並びを入口にしています。既存のスクリプトはそのまま動作し、実行結果も変わりません。
|
- スライダーと文字列パラメータは、スクリプト本体でそれらを参照したときだけエフェクトパネルへ
|
||||||
- 変換はすべての実行エンジンに適用され、LuaJIT、MoonSharp、CPU カーネルのいずれでも同じ結果になります。文字列とコメントの内側は変換しません。
|
表示されます。参照しない既存スクリプトの表示と挙動は変わりません。
|
||||||
- 分岐の各辺、ラムダの本体、パイプの対象、補間文字列の {式}、範囲判定の両端など、入れ子になった式にも構文拡張の変換を再帰的に適用します。
|
- CPU/GPU カーネルは数値計算に特化するため、文字列パラメータは扱いません。文字列を参照する
|
||||||
- v1.15.0 までの仕様に変更はありません。obj.pixelshader をはじめとする既存の API と、実行エンジンの自動振り分けに変更はありません。
|
スクリプトは MoonSharp かネイティブで実行してください。
|
||||||
|
- トラックバー・チェックボックス・カラー・AviUtl 互換ヘッダの既存挙動は変わりません。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 内部実装
|
## 内部実装
|
||||||
|
|
||||||
- 字句解析器に補間文字列のトークン種別を追加し、バッククォートで囲む文字列を 1 つのトークンとして読み取ります。あわせて演算子として => と |> を追加しました。
|
- ネイティブの共有メモリへ文字列パラメータ領域を追加し、容量をワーカーの起動引数にしました。
|
||||||
- 範囲の解析を共通のヘルパーへ切り出しました。開始・終了・増分と、終端を含むかどうかを 1 か所で判定し、範囲 for と範囲判定の両方がこのヘルパーを使います。
|
必要量が容量を超えると 2 倍へ拡張し、容量の半分以下に収まると縮小します。最小は 64KB で、
|
||||||
- 各記法は、属性で優先順位を与えた変換規則として実装しました。nil 条件演算子の規則は三項演算子より先に走り、?. と ?[ を単独の ? より先に処理します。既存の変換基盤はそのまま流用し、変換文脈に変更はありません。
|
容量の変化はワーカーの再生成のみで完結します。
|
||||||
- ドキュメントは付属の 8 言語、サイトの構文拡張のページ、README を更新しました。
|
- 文字列パラメータの変更検出をバージョン方式へ変更し、フレームごとの署名文字列の生成を
|
||||||
- テストは 3297 件です。v1.15.0 の 3259 件から、三項演算子、nil 条件演算子、ラムダ式、パイプ、補間文字列、範囲判定の各変換と評価、入れ子になった式の変換、比較演算子や for のヘッダーを変換しないこと、対象や左辺を 1 度だけ評価することの検証を追加しました。
|
やめました。値の参照が変わったときだけ版数を進めます。
|
||||||
|
- ネイティブの文字列書き込みで、名前のバイト列をキャッシュし、値のエンコード先バッファを
|
||||||
|
再利用します。
|
||||||
|
- スライダーの値は MoonSharp の obj テーブル・ネイティブのフィールド・CPU/GPU カーネルの
|
||||||
|
ユニフォームへ配線しました。スクリプトの使用検出は obj メンバを汎用的に走査する方式へ
|
||||||
|
再設計しました。
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
========================================
|
========================================
|
||||||
Luaスクリプト for YMM4 v1.16.0
|
Luaスクリプト for YMM4 v1.10.0
|
||||||
========================================
|
========================================
|
||||||
YukkuriMovieMaker4(YMM4)上で動作する、Luaスクリプトで映像エフェクトのパラメータや
|
YukkuriMovieMaker4(YMM4)上で動作する、Luaスクリプトで映像エフェクトのパラメータや
|
||||||
ピクセルを直接制御できるプラグインです。
|
ピクセルを直接制御できるプラグインです。
|
||||||
|
|||||||
@@ -73,10 +73,6 @@ jobs:
|
|||||||
New-Item -ItemType Directory -Path "$stageRoot/LICENSE" -Force | Out-Null
|
New-Item -ItemType Directory -Path "$stageRoot/LICENSE" -Force | Out-Null
|
||||||
|
|
||||||
Copy-Item "$outDir/LuaScript.dll" "$stageRoot/"
|
Copy-Item "$outDir/LuaScript.dll" "$stageRoot/"
|
||||||
Copy-Item "$outDir/LuaScript.Kernel.dll" "$stageRoot/"
|
|
||||||
Copy-Item "$outDir/LuaScript.Compat.dll" "$stageRoot/"
|
|
||||||
Copy-Item "$outDir/LuaScript.Media.dll" "$stageRoot/"
|
|
||||||
Copy-Item "$outDir/LuaScript.Runtime.dll" "$stageRoot/"
|
|
||||||
Copy-Item "$outDir/MoonSharp.Interpreter.dll" "$stageRoot/"
|
Copy-Item "$outDir/MoonSharp.Interpreter.dll" "$stageRoot/"
|
||||||
Copy-Item "$outDir/native/luajit.exe" "$stageRoot/native/"
|
Copy-Item "$outDir/native/luajit.exe" "$stageRoot/native/"
|
||||||
Copy-Item "$outDir/native/lua51.dll" "$stageRoot/native/"
|
Copy-Item "$outDir/native/lua51.dll" "$stageRoot/native/"
|
||||||
|
|||||||
@@ -1,137 +0,0 @@
|
|||||||
namespace LuaScript.Compat
|
|
||||||
{
|
|
||||||
internal static class AviUtlGlobalFunctions
|
|
||||||
{
|
|
||||||
public static int BitOr(double a, double b) => ToBitOperand(a) | ToBitOperand(b);
|
|
||||||
|
|
||||||
public static int BitAnd(double a, double b) => ToBitOperand(a) & ToBitOperand(b);
|
|
||||||
|
|
||||||
public static int BitXor(double a, double b) => ToBitOperand(a) ^ ToBitOperand(b);
|
|
||||||
|
|
||||||
public static int BitShift(double value, double shift)
|
|
||||||
{
|
|
||||||
int v = ToBitOperand(value);
|
|
||||||
int n = ToBitOperand(shift);
|
|
||||||
return n >= 0 ? v << n : v >> -n;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int ToBitOperand(double value)
|
|
||||||
{
|
|
||||||
if (double.IsNaN(value))
|
|
||||||
return 0;
|
|
||||||
if (value <= int.MinValue)
|
|
||||||
return int.MinValue;
|
|
||||||
if (value >= int.MaxValue)
|
|
||||||
return int.MaxValue;
|
|
||||||
return (int)Math.Ceiling(value - 0.5);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int RgbCompose(double r, double g, double b) =>
|
|
||||||
(ToColorChannel(r) << 16) | (ToColorChannel(g) << 8) | ToColorChannel(b);
|
|
||||||
|
|
||||||
public static void RgbComponents(double color, out int r, out int g, out int b)
|
|
||||||
{
|
|
||||||
int c = ToColor(color);
|
|
||||||
r = (c >> 16) & 0xFF;
|
|
||||||
g = (c >> 8) & 0xFF;
|
|
||||||
b = c & 0xFF;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int RgbInterpolate(double r1, double g1, double b1, double r2, double g2, double b2, double t) =>
|
|
||||||
RgbCompose(r1 + (r2 - r1) * t, g1 + (g2 - g1) * t, b1 + (b2 - b1) * t);
|
|
||||||
|
|
||||||
public static int HsvCompose(double h, double s, double v)
|
|
||||||
{
|
|
||||||
h = WrapHue(h);
|
|
||||||
s = ClampRatio(s);
|
|
||||||
v = ClampRatio(v);
|
|
||||||
|
|
||||||
double c = v * s;
|
|
||||||
double x = c * (1d - Math.Abs(h / 60d % 2d - 1d));
|
|
||||||
double m = v - c;
|
|
||||||
int sector = (int)Math.Floor(h / 60d);
|
|
||||||
|
|
||||||
double r, g, b;
|
|
||||||
if (sector == 0) { r = c; g = x; b = 0d; }
|
|
||||||
else if (sector == 1) { r = x; g = c; b = 0d; }
|
|
||||||
else if (sector == 2) { r = 0d; g = c; b = x; }
|
|
||||||
else if (sector == 3) { r = 0d; g = x; b = c; }
|
|
||||||
else if (sector == 4) { r = x; g = 0d; b = c; }
|
|
||||||
else { r = c; g = 0d; b = x; }
|
|
||||||
|
|
||||||
return (ToColorChannel(RoundHalfUp((r + m) * 255d)) << 16) |
|
|
||||||
(ToColorChannel(RoundHalfUp((g + m) * 255d)) << 8) |
|
|
||||||
ToColorChannel(RoundHalfUp((b + m) * 255d));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void HsvComponents(double color, out int h, out int s, out int v)
|
|
||||||
{
|
|
||||||
RgbComponents(color, out int r, out int g, out int b);
|
|
||||||
double rf = r / 255d;
|
|
||||||
double gf = g / 255d;
|
|
||||||
double bf = b / 255d;
|
|
||||||
|
|
||||||
double max = Math.Max(rf, Math.Max(gf, bf));
|
|
||||||
double min = Math.Min(rf, Math.Min(gf, bf));
|
|
||||||
double delta = max - min;
|
|
||||||
|
|
||||||
double hue = 0d;
|
|
||||||
if (delta > 0d)
|
|
||||||
{
|
|
||||||
if (max == rf)
|
|
||||||
hue = 60d * (((gf - bf) / delta % 6d + 6d) % 6d);
|
|
||||||
else if (max == gf)
|
|
||||||
hue = 60d * ((bf - rf) / delta + 2d);
|
|
||||||
else
|
|
||||||
hue = 60d * ((rf - gf) / delta + 4d);
|
|
||||||
}
|
|
||||||
|
|
||||||
h = (int)RoundHalfUp(hue);
|
|
||||||
s = (int)RoundHalfUp(max > 0d ? delta / max * 100d : 0d);
|
|
||||||
v = (int)RoundHalfUp(max * 100d);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int HsvInterpolate(double h1, double s1, double v1, double h2, double s2, double v2, double t) =>
|
|
||||||
HsvCompose(h1 + (h2 - h1) * t, s1 + (s2 - s1) * t, v1 + (v2 - v1) * t);
|
|
||||||
|
|
||||||
private static int ToColorChannel(double value)
|
|
||||||
{
|
|
||||||
if (double.IsNaN(value) || value <= 0d)
|
|
||||||
return 0;
|
|
||||||
if (value >= 255d)
|
|
||||||
return 255;
|
|
||||||
return (int)Math.Floor(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int ToColor(double value)
|
|
||||||
{
|
|
||||||
if (double.IsNaN(value))
|
|
||||||
return 0;
|
|
||||||
if (value <= int.MinValue)
|
|
||||||
value = int.MinValue;
|
|
||||||
else if (value >= int.MaxValue)
|
|
||||||
value = int.MaxValue;
|
|
||||||
long floored = (long)Math.Floor(value);
|
|
||||||
return (int)(floored & 0xFFFFFF);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static double WrapHue(double h)
|
|
||||||
{
|
|
||||||
if (double.IsNaN(h) || double.IsInfinity(h))
|
|
||||||
return 0d;
|
|
||||||
double wrapped = h - Math.Floor(h / 360d) * 360d;
|
|
||||||
return wrapped >= 360d ? 0d : wrapped;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static double ClampRatio(double value)
|
|
||||||
{
|
|
||||||
if (double.IsNaN(value) || value <= 0d)
|
|
||||||
return 0d;
|
|
||||||
if (value >= 100d)
|
|
||||||
return 1d;
|
|
||||||
return value / 100d;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static double RoundHalfUp(double value) => Math.Floor(value + 0.5);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>net10.0-windows10.0.19041.0</TargetFramework>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
<Version>1.16.0</Version>
|
|
||||||
<Platforms>AnyCPU;x64</Platforms>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<InternalsVisibleTo Include="LuaScript" />
|
|
||||||
<InternalsVisibleTo Include="LuaScript.Runtime" />
|
|
||||||
<InternalsVisibleTo Include="LuaScript.Tests" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\LuaScript.Kernel\LuaScript.Kernel.csproj" />
|
|
||||||
<ProjectReference Include="..\LuaScript.Generator\LuaScript.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<EmbeddedResource Include="AviUtlCompatMap.xml" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
namespace LuaScript.Compat
|
|
||||||
{
|
|
||||||
internal enum AviUtlEngine
|
|
||||||
{
|
|
||||||
AviUtl,
|
|
||||||
AviUtl2,
|
|
||||||
Both,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
namespace LuaScript.Compat
|
|
||||||
{
|
|
||||||
internal readonly record struct AviUtlParameterMapping(string Source, string Property, double Scale, double Offset)
|
|
||||||
{
|
|
||||||
public double Transform(double value) => value * Scale + Offset;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace LuaScript.Compat
|
|
||||||
{
|
|
||||||
internal sealed class AviUtlPixelShaderLibrary
|
|
||||||
{
|
|
||||||
private const string SectionMarker = "--[[pixelshader@";
|
|
||||||
|
|
||||||
public static AviUtlPixelShaderLibrary Empty { get; } = new(null);
|
|
||||||
|
|
||||||
private readonly Dictionary<string, string>? _shaders;
|
|
||||||
|
|
||||||
private AviUtlPixelShaderLibrary(Dictionary<string, string>? shaders) => _shaders = shaders;
|
|
||||||
|
|
||||||
public int Count => _shaders?.Count ?? 0;
|
|
||||||
|
|
||||||
public static AviUtlPixelShaderLibrary Parse(string? source)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(source))
|
|
||||||
return Empty;
|
|
||||||
|
|
||||||
Dictionary<string, string>? shaders = null;
|
|
||||||
int index = 0;
|
|
||||||
while (index < source.Length)
|
|
||||||
{
|
|
||||||
int lineStart = index;
|
|
||||||
int lineBreak = source.IndexOf('\n', index);
|
|
||||||
index = lineBreak < 0 ? source.Length : lineBreak + 1;
|
|
||||||
|
|
||||||
int i = ScriptParserHelper.SkipSpaces(source, lineStart);
|
|
||||||
if (!MatchesMarker(source, i))
|
|
||||||
continue;
|
|
||||||
i += SectionMarker.Length;
|
|
||||||
|
|
||||||
int nameStart = i;
|
|
||||||
while (i < source.Length && IsEntryPointChar(source[i]))
|
|
||||||
i++;
|
|
||||||
if (i == nameStart || i >= source.Length || source[i] != ':')
|
|
||||||
continue;
|
|
||||||
|
|
||||||
string name = source[nameStart..i];
|
|
||||||
int bodyStart = i + 1;
|
|
||||||
int bodyEnd = source.IndexOf("]]", bodyStart, StringComparison.Ordinal);
|
|
||||||
if (bodyEnd < 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
shaders ??= new Dictionary<string, string>(StringComparer.Ordinal);
|
|
||||||
shaders.TryAdd(name, source[bodyStart..bodyEnd]);
|
|
||||||
index = bodyEnd + 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
return shaders is null ? Empty : new AviUtlPixelShaderLibrary(shaders);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryGet(string name, out string hlsl)
|
|
||||||
{
|
|
||||||
hlsl = string.Empty;
|
|
||||||
if (_shaders is null || string.IsNullOrEmpty(name))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int at = name.IndexOf('@');
|
|
||||||
string local = at < 0 ? name : name[..at];
|
|
||||||
if (_shaders.TryGetValue(local, out var found))
|
|
||||||
{
|
|
||||||
hlsl = found;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string EntryPointOf(string name)
|
|
||||||
{
|
|
||||||
int at = name.IndexOf('@');
|
|
||||||
return at < 0 ? name : name[..at];
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool MatchesMarker(string text, int index) =>
|
|
||||||
index + SectionMarker.Length <= text.Length &&
|
|
||||||
string.CompareOrdinal(text, index, SectionMarker, 0, SectionMarker.Length) == 0;
|
|
||||||
|
|
||||||
private static bool IsEntryPointChar(char c) =>
|
|
||||||
ScriptParserHelper.IsNameChar(c) || c == '_';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
using LuaScript.Compat.Syntax;
|
|
||||||
|
|
||||||
namespace LuaScript.Compat
|
|
||||||
{
|
|
||||||
internal static class LuaSyntaxExtensions
|
|
||||||
{
|
|
||||||
public static string Rewrite(string source) => RewriteWithMap(source).Code;
|
|
||||||
|
|
||||||
public static (string Code, int[] ChangedLines) RewriteWithMap(string source)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(source))
|
|
||||||
return (source, []);
|
|
||||||
|
|
||||||
var tokens = LuaSyntaxLexer.Tokenize(source);
|
|
||||||
var context = new LuaRewriteContext(tokens);
|
|
||||||
var rules = LuaSyntaxRuleRegistry.Rules;
|
|
||||||
|
|
||||||
while (!context.AtEnd)
|
|
||||||
{
|
|
||||||
var current = context.Current;
|
|
||||||
if (current.IsTrivia || current.Kind == LuaSyntaxTokenKind.String)
|
|
||||||
{
|
|
||||||
context.Emit(current);
|
|
||||||
context.Advance();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool matched = false;
|
|
||||||
foreach (var rule in rules)
|
|
||||||
{
|
|
||||||
if (rule.TryApply(context))
|
|
||||||
{
|
|
||||||
matched = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!matched)
|
|
||||||
{
|
|
||||||
context.Emit(current);
|
|
||||||
context.Advance();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return context.Changed ? (context.Serialize(), context.ChangedLines) : (source, []);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
namespace LuaScript.Compat
|
|
||||||
{
|
|
||||||
internal static class ScriptParserHelper
|
|
||||||
{
|
|
||||||
public static bool IsSectionHeader(string line)
|
|
||||||
{
|
|
||||||
int i = SkipSpaces(line, 0);
|
|
||||||
return i < line.Length && line[i] == '@';
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int SkipSpaces(string text, int index)
|
|
||||||
{
|
|
||||||
while (index < text.Length && (text[index] == ' ' || text[index] == '\t'))
|
|
||||||
index++;
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool IsNameChar(char c) =>
|
|
||||||
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
|
|
||||||
|
|
||||||
public static string[] SplitLines(string source) =>
|
|
||||||
source.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');
|
|
||||||
|
|
||||||
public static bool TryFindSection(string[] lines, out int start, out int end)
|
|
||||||
{
|
|
||||||
start = 0;
|
|
||||||
end = lines.Length;
|
|
||||||
for (int i = 0; i < lines.Length; i++)
|
|
||||||
{
|
|
||||||
if (!IsSectionHeader(lines[i]))
|
|
||||||
continue;
|
|
||||||
start = i + 1;
|
|
||||||
end = lines.Length;
|
|
||||||
for (int j = start; j < lines.Length; j++)
|
|
||||||
{
|
|
||||||
if (IsSectionHeader(lines[j]))
|
|
||||||
{
|
|
||||||
end = j;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool TryParseDirective(string line, out string name, out string content)
|
|
||||||
{
|
|
||||||
name = string.Empty;
|
|
||||||
content = string.Empty;
|
|
||||||
int i = SkipSpaces(line, 0);
|
|
||||||
if (i + 1 >= line.Length || line[i] != '-' || line[i + 1] != '-')
|
|
||||||
return false;
|
|
||||||
i += 2;
|
|
||||||
if (i < line.Length && line[i] == '!')
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int nameStart = i;
|
|
||||||
while (i < line.Length && IsNameChar(line[i]))
|
|
||||||
i++;
|
|
||||||
if (i >= line.Length || line[i] != ':')
|
|
||||||
return false;
|
|
||||||
|
|
||||||
name = line.Substring(nameStart, i - nameStart);
|
|
||||||
content = line.Substring(i + 1);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
namespace LuaScript.Compat.Syntax
|
|
||||||
{
|
|
||||||
internal interface ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
bool TryApply(LuaRewriteContext context);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace LuaScript.Compat.Syntax
|
|
||||||
{
|
|
||||||
internal static class LuaKeywords
|
|
||||||
{
|
|
||||||
private static readonly HashSet<string> Reserved = new()
|
|
||||||
{
|
|
||||||
"and", "break", "do", "else", "elseif", "end", "false", "for", "function",
|
|
||||||
"goto", "if", "in", "local", "nil", "not", "or", "repeat", "return",
|
|
||||||
"then", "true", "until", "while",
|
|
||||||
};
|
|
||||||
|
|
||||||
private static readonly HashSet<string> ExpressionSafe = new()
|
|
||||||
{
|
|
||||||
"and", "or", "not", "nil", "true", "false",
|
|
||||||
};
|
|
||||||
|
|
||||||
public static bool IsReserved(string name) => Reserved.Contains(name);
|
|
||||||
|
|
||||||
public static bool IsExpressionBoundary(string name) => Reserved.Contains(name) && !ExpressionSafe.Contains(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,130 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace LuaScript.Compat.Syntax
|
|
||||||
{
|
|
||||||
internal readonly struct LuaRangeParts
|
|
||||||
{
|
|
||||||
public LuaRangeParts(string start, string stop, string? step, bool exclusive)
|
|
||||||
{
|
|
||||||
Start = start;
|
|
||||||
Stop = stop;
|
|
||||||
Step = step;
|
|
||||||
Exclusive = exclusive;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Start { get; }
|
|
||||||
|
|
||||||
public string Stop { get; }
|
|
||||||
|
|
||||||
public string? Step { get; }
|
|
||||||
|
|
||||||
public bool Exclusive { get; }
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static class LuaRangeSyntax
|
|
||||||
{
|
|
||||||
public static bool TryParse(IReadOnlyList<LuaSyntaxToken> input, int open, out LuaRangeParts parts, out int end)
|
|
||||||
{
|
|
||||||
parts = default;
|
|
||||||
end = open;
|
|
||||||
|
|
||||||
if (open < 0 || open >= input.Count || !input[open].IsOperator("<"))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int position = open + 1;
|
|
||||||
if (!TryReadSegment(input, ref position, "..", ">", out string start, out string startTerminator) || startTerminator != "..")
|
|
||||||
return false;
|
|
||||||
|
|
||||||
bool exclusive = false;
|
|
||||||
int caret = NextSignificant(input, position);
|
|
||||||
if (caret >= 0 && input[caret].IsOperator("<"))
|
|
||||||
{
|
|
||||||
exclusive = true;
|
|
||||||
position = caret + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!TryReadSegment(input, ref position, ",", ">", out string stop, out string stopTerminator))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
string? step = null;
|
|
||||||
if (stopTerminator == ",")
|
|
||||||
{
|
|
||||||
if (!TryReadSegment(input, ref position, ">", ">", out string stepText, out string stepTerminator) || stepTerminator != ">")
|
|
||||||
return false;
|
|
||||||
step = stepText;
|
|
||||||
}
|
|
||||||
else if (stopTerminator != ">")
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (start.Length == 0 || stop.Length == 0 || (step is not null && step.Length == 0))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
parts = new LuaRangeParts(start, stop, step, exclusive);
|
|
||||||
end = position;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool TryReadSegment(IReadOnlyList<LuaSyntaxToken> input, ref int position, string primaryStop, string secondaryStop, out string text, out string terminator)
|
|
||||||
{
|
|
||||||
text = string.Empty;
|
|
||||||
terminator = string.Empty;
|
|
||||||
int start = position;
|
|
||||||
int depth = 0;
|
|
||||||
int count = input.Count;
|
|
||||||
|
|
||||||
while (position < count)
|
|
||||||
{
|
|
||||||
var token = input[position];
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Newline && depth == 0)
|
|
||||||
return false;
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Operator)
|
|
||||||
{
|
|
||||||
if (token.Text is "(" or "[" or "{")
|
|
||||||
{
|
|
||||||
depth++;
|
|
||||||
}
|
|
||||||
else if (token.Text is ")" or "]" or "}")
|
|
||||||
{
|
|
||||||
if (depth == 0)
|
|
||||||
return false;
|
|
||||||
depth--;
|
|
||||||
}
|
|
||||||
else if (depth == 0 && (token.Text == primaryStop || token.Text == secondaryStop))
|
|
||||||
{
|
|
||||||
terminator = token.Text;
|
|
||||||
text = Trim(input, start, position);
|
|
||||||
position++;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
position++;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int NextSignificant(IReadOnlyList<LuaSyntaxToken> input, int from)
|
|
||||||
{
|
|
||||||
for (int i = from; i < input.Count; i++)
|
|
||||||
{
|
|
||||||
if (!input[i].IsTrivia)
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string Trim(IReadOnlyList<LuaSyntaxToken> input, int begin, int end)
|
|
||||||
{
|
|
||||||
while (begin < end && input[begin].IsTrivia)
|
|
||||||
begin++;
|
|
||||||
while (end > begin && input[end - 1].IsTrivia)
|
|
||||||
end--;
|
|
||||||
var builder = new StringBuilder();
|
|
||||||
for (int i = begin; i < end; i++)
|
|
||||||
builder.Append(input[i].Text);
|
|
||||||
return builder.ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,318 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace LuaScript.Compat.Syntax
|
|
||||||
{
|
|
||||||
internal sealed class LuaRewriteContext
|
|
||||||
{
|
|
||||||
private readonly List<LuaSyntaxToken> _input;
|
|
||||||
private readonly List<LuaSyntaxToken> _output;
|
|
||||||
private List<int>? _changedLines;
|
|
||||||
private int _line = 1;
|
|
||||||
private int _lastChangedLine;
|
|
||||||
private int _temporaries;
|
|
||||||
|
|
||||||
public LuaRewriteContext(List<LuaSyntaxToken> input)
|
|
||||||
{
|
|
||||||
_input = input;
|
|
||||||
_output = new List<LuaSyntaxToken>(input.Count);
|
|
||||||
}
|
|
||||||
|
|
||||||
public IReadOnlyList<LuaSyntaxToken> Input => _input;
|
|
||||||
|
|
||||||
public List<LuaSyntaxToken> Output => _output;
|
|
||||||
|
|
||||||
public int Index { get; set; }
|
|
||||||
|
|
||||||
public bool Changed { get; private set; }
|
|
||||||
|
|
||||||
public int[] ChangedLines => _changedLines is null ? [] : _changedLines.ToArray();
|
|
||||||
|
|
||||||
public bool AtEnd => Index >= _input.Count;
|
|
||||||
|
|
||||||
public LuaSyntaxToken Current => _input[Index];
|
|
||||||
|
|
||||||
public void Advance() => Index++;
|
|
||||||
|
|
||||||
public void Emit(LuaSyntaxToken token)
|
|
||||||
{
|
|
||||||
_output.Add(token);
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Newline)
|
|
||||||
_line++;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void EmitRaw(string text)
|
|
||||||
{
|
|
||||||
_output.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.Name, text));
|
|
||||||
MarkChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void EmitOperator(string text)
|
|
||||||
{
|
|
||||||
_output.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.Operator, text));
|
|
||||||
MarkChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public string NextTemporary() => "__lc" + _temporaries++;
|
|
||||||
|
|
||||||
public int NextSignificant(int from)
|
|
||||||
{
|
|
||||||
for (int i = from; i < _input.Count; i++)
|
|
||||||
{
|
|
||||||
if (!_input[i].IsTrivia)
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryPreviousOutputSignificantSameLine(out LuaSyntaxToken token)
|
|
||||||
{
|
|
||||||
for (int i = _output.Count - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
if (_output[i].Kind == LuaSyntaxTokenKind.Newline)
|
|
||||||
break;
|
|
||||||
if (_output[i].IsTrivia)
|
|
||||||
continue;
|
|
||||||
token = _output[i];
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
token = default;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int NextSignificantSameLine(int from)
|
|
||||||
{
|
|
||||||
for (int i = from; i < _input.Count; i++)
|
|
||||||
{
|
|
||||||
if (_input[i].Kind == LuaSyntaxTokenKind.Newline)
|
|
||||||
return -1;
|
|
||||||
if (_input[i].IsTrivia)
|
|
||||||
continue;
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void EmitWord(string word)
|
|
||||||
{
|
|
||||||
if (_output.Count > 0 && !_output[_output.Count - 1].IsTrivia)
|
|
||||||
_output.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.Whitespace, " "));
|
|
||||||
_output.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.Name, word));
|
|
||||||
if (Index < _input.Count && !_input[Index].IsTrivia)
|
|
||||||
_output.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.Whitespace, " "));
|
|
||||||
MarkChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void MarkChanged()
|
|
||||||
{
|
|
||||||
Changed = true;
|
|
||||||
if (_line == _lastChangedLine)
|
|
||||||
return;
|
|
||||||
(_changedLines ??= new List<int>()).Add(_line);
|
|
||||||
_lastChangedLine = _line;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryReadTrailingLvalue(out int start, out string text)
|
|
||||||
{
|
|
||||||
start = _output.Count;
|
|
||||||
text = string.Empty;
|
|
||||||
int end = _output.Count;
|
|
||||||
while (end > 0 && _output[end - 1].IsTrivia)
|
|
||||||
end--;
|
|
||||||
if (end == 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int depth = 0;
|
|
||||||
int begin = end;
|
|
||||||
for (int i = end - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
var token = _output[i];
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Operator)
|
|
||||||
{
|
|
||||||
if (token.Text is ")" or "]" or "}")
|
|
||||||
{
|
|
||||||
depth++;
|
|
||||||
begin = i;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (token.Text is "(" or "[" or "{")
|
|
||||||
{
|
|
||||||
if (depth == 0)
|
|
||||||
break;
|
|
||||||
depth--;
|
|
||||||
begin = i;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (depth > 0 || token.Text == ".")
|
|
||||||
{
|
|
||||||
begin = i;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Name)
|
|
||||||
{
|
|
||||||
if (depth == 0 && LuaKeywords.IsReserved(token.Text))
|
|
||||||
break;
|
|
||||||
begin = i;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (depth > 0)
|
|
||||||
{
|
|
||||||
begin = i;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (begin < end && _output[begin].IsTrivia)
|
|
||||||
begin++;
|
|
||||||
if (begin >= end)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
start = begin;
|
|
||||||
text = Concat(begin, end);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryPopOrOperand(out string text)
|
|
||||||
{
|
|
||||||
text = string.Empty;
|
|
||||||
int end = _output.Count;
|
|
||||||
while (end > 0 && _output[end - 1].IsTrivia)
|
|
||||||
end--;
|
|
||||||
if (end == 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int depth = 0;
|
|
||||||
int begin = end;
|
|
||||||
for (int i = end - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
var token = _output[i];
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Operator)
|
|
||||||
{
|
|
||||||
if (token.Text is ")" or "]" or "}")
|
|
||||||
{
|
|
||||||
depth++;
|
|
||||||
begin = i;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (token.Text is "(" or "[" or "{")
|
|
||||||
{
|
|
||||||
if (depth == 0)
|
|
||||||
break;
|
|
||||||
depth--;
|
|
||||||
begin = i;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (depth == 0 && token.Text is "=" or "," or ";")
|
|
||||||
break;
|
|
||||||
begin = i;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Newline && depth == 0)
|
|
||||||
break;
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Name && depth == 0 && LuaKeywords.IsExpressionBoundary(token.Text))
|
|
||||||
break;
|
|
||||||
begin = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (begin < end && _output[begin].IsTrivia)
|
|
||||||
begin++;
|
|
||||||
if (begin >= end)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
text = Concat(begin, end);
|
|
||||||
_output.RemoveRange(begin, _output.Count - begin);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RemoveOutputFrom(int start) => _output.RemoveRange(start, _output.Count - start);
|
|
||||||
|
|
||||||
public bool LastOutputIsTrivia() => _output.Count == 0 || _output[_output.Count - 1].IsTrivia;
|
|
||||||
|
|
||||||
public string ReadLineExpression()
|
|
||||||
{
|
|
||||||
int start = Index;
|
|
||||||
int depth = 0;
|
|
||||||
while (Index < _input.Count)
|
|
||||||
{
|
|
||||||
var token = _input[Index];
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Newline && depth == 0)
|
|
||||||
break;
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Operator)
|
|
||||||
{
|
|
||||||
if (token.Text is "(" or "[" or "{")
|
|
||||||
depth++;
|
|
||||||
else if (token.Text is ")" or "]" or "}")
|
|
||||||
{
|
|
||||||
if (depth == 0)
|
|
||||||
break;
|
|
||||||
depth--;
|
|
||||||
}
|
|
||||||
else if (depth == 0 && token.Text == ";")
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Index++;
|
|
||||||
}
|
|
||||||
return TrimmedRange(start, Index);
|
|
||||||
}
|
|
||||||
|
|
||||||
public string ReadForwardOrOperand()
|
|
||||||
{
|
|
||||||
int start = Index;
|
|
||||||
int depth = 0;
|
|
||||||
while (Index < _input.Count)
|
|
||||||
{
|
|
||||||
var token = _input[Index];
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Newline && depth == 0)
|
|
||||||
break;
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Operator)
|
|
||||||
{
|
|
||||||
if (token.Text is "(" or "[" or "{")
|
|
||||||
depth++;
|
|
||||||
else if (token.Text is ")" or "]" or "}")
|
|
||||||
{
|
|
||||||
if (depth == 0)
|
|
||||||
break;
|
|
||||||
depth--;
|
|
||||||
}
|
|
||||||
else if (depth == 0 && token.Text is "," or ";")
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (token.Kind == LuaSyntaxTokenKind.Name && depth == 0 && LuaKeywords.IsExpressionBoundary(token.Text))
|
|
||||||
break;
|
|
||||||
Index++;
|
|
||||||
}
|
|
||||||
return TrimmedRange(start, Index);
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Serialize()
|
|
||||||
{
|
|
||||||
var builder = new StringBuilder();
|
|
||||||
foreach (var token in _output)
|
|
||||||
builder.Append(token.Text);
|
|
||||||
return builder.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private string Concat(int begin, int end)
|
|
||||||
{
|
|
||||||
var builder = new StringBuilder();
|
|
||||||
for (int i = begin; i < end; i++)
|
|
||||||
builder.Append(_output[i].Text);
|
|
||||||
return builder.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private string TrimmedRange(int begin, int end)
|
|
||||||
{
|
|
||||||
while (begin < end && _input[begin].IsTrivia)
|
|
||||||
begin++;
|
|
||||||
while (end > begin && _input[end - 1].IsTrivia)
|
|
||||||
end--;
|
|
||||||
var builder = new StringBuilder();
|
|
||||||
for (int i = begin; i < end; i++)
|
|
||||||
builder.Append(_input[i].Text);
|
|
||||||
return builder.ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
namespace LuaScript.Compat.Syntax
|
|
||||||
{
|
|
||||||
internal static class LuaSyntaxFacts
|
|
||||||
{
|
|
||||||
public static bool IsValueEnd(LuaSyntaxToken token) => token.Kind switch
|
|
||||||
{
|
|
||||||
LuaSyntaxTokenKind.Number or LuaSyntaxTokenKind.String => true,
|
|
||||||
LuaSyntaxTokenKind.Name => !LuaKeywords.IsReserved(token.Text) || token.Text is "nil" or "true" or "false",
|
|
||||||
LuaSyntaxTokenKind.Operator => token.Text is ")" or "]" or "}",
|
|
||||||
_ => false,
|
|
||||||
};
|
|
||||||
|
|
||||||
public static bool IsValueStart(LuaSyntaxToken token) => token.Kind switch
|
|
||||||
{
|
|
||||||
LuaSyntaxTokenKind.Number or LuaSyntaxTokenKind.String => true,
|
|
||||||
LuaSyntaxTokenKind.Name => !LuaKeywords.IsReserved(token.Text) || token.Text is "nil" or "true" or "false" or "not" or "function",
|
|
||||||
LuaSyntaxTokenKind.Operator => token.Text is "(" or "{" or "-" or "#" or "~",
|
|
||||||
_ => false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,246 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace LuaScript.Compat.Syntax
|
|
||||||
{
|
|
||||||
internal static class LuaSyntaxLexer
|
|
||||||
{
|
|
||||||
private static readonly string[] Operators =
|
|
||||||
{
|
|
||||||
"...", "..=", "??=", "||=", "&&=",
|
|
||||||
"==", "~=", "<=", ">=", "..", "::", "??", "||", "&&", "!=", "++",
|
|
||||||
"=>", "|>",
|
|
||||||
"+=", "-=", "*=", "/=", "%=", "^=",
|
|
||||||
"+", "-", "*", "/", "%", "^", "#", "<", ">", "=",
|
|
||||||
"(", ")", "{", "}", "[", "]", ";", ":", ",", ".",
|
|
||||||
"!", "&", "|", "?", "~",
|
|
||||||
};
|
|
||||||
|
|
||||||
public static List<LuaSyntaxToken> Tokenize(string source)
|
|
||||||
{
|
|
||||||
var tokens = new List<LuaSyntaxToken>();
|
|
||||||
int i = 0;
|
|
||||||
int length = source.Length;
|
|
||||||
|
|
||||||
while (i < length)
|
|
||||||
{
|
|
||||||
char c = source[i];
|
|
||||||
|
|
||||||
if (c == '\r' || c == '\n')
|
|
||||||
{
|
|
||||||
int start = i;
|
|
||||||
if (c == '\r' && i + 1 < length && source[i + 1] == '\n')
|
|
||||||
i += 2;
|
|
||||||
else
|
|
||||||
i++;
|
|
||||||
tokens.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.Newline, source.Substring(start, i - start)));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == ' ' || c == '\t' || c == '\f' || c == '\v')
|
|
||||||
{
|
|
||||||
int start = i;
|
|
||||||
while (i < length && (source[i] == ' ' || source[i] == '\t' || source[i] == '\f' || source[i] == '\v'))
|
|
||||||
i++;
|
|
||||||
tokens.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.Whitespace, source.Substring(start, i - start)));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == '-' && i + 1 < length && source[i + 1] == '-')
|
|
||||||
{
|
|
||||||
int start = i;
|
|
||||||
i = SkipComment(source, i + 2);
|
|
||||||
tokens.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.Comment, source.Substring(start, i - start)));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == '[')
|
|
||||||
{
|
|
||||||
int level = LongBracketLevel(source, i);
|
|
||||||
if (level >= 0)
|
|
||||||
{
|
|
||||||
int start = i;
|
|
||||||
i = SkipLongBracket(source, i, level);
|
|
||||||
tokens.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.String, source.Substring(start, i - start)));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == '"' || c == '\'')
|
|
||||||
{
|
|
||||||
int start = i;
|
|
||||||
i = SkipShortString(source, i);
|
|
||||||
tokens.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.String, source.Substring(start, i - start)));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == '`')
|
|
||||||
{
|
|
||||||
int start = i;
|
|
||||||
i = SkipShortString(source, i);
|
|
||||||
tokens.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.InterpolatedString, source.Substring(start, i - start)));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (IsDigit(c) || (c == '.' && i + 1 < length && IsDigit(source[i + 1])))
|
|
||||||
{
|
|
||||||
int start = i;
|
|
||||||
i = SkipNumber(source, i);
|
|
||||||
tokens.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.Number, source.Substring(start, i - start)));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (IsNameStart(c))
|
|
||||||
{
|
|
||||||
int start = i;
|
|
||||||
while (i < length && IsNamePart(source[i]))
|
|
||||||
i++;
|
|
||||||
tokens.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.Name, source.Substring(start, i - start)));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
string? op = MatchOperator(source, i);
|
|
||||||
if (op is not null)
|
|
||||||
{
|
|
||||||
tokens.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.Operator, op));
|
|
||||||
i += op.Length;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
tokens.Add(new LuaSyntaxToken(LuaSyntaxTokenKind.Operator, source.Substring(i, 1)));
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return tokens;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string? MatchOperator(string source, int index)
|
|
||||||
{
|
|
||||||
int remaining = source.Length - index;
|
|
||||||
foreach (var op in Operators)
|
|
||||||
{
|
|
||||||
if (op.Length > remaining)
|
|
||||||
continue;
|
|
||||||
if (string.CompareOrdinal(source, index, op, 0, op.Length) == 0)
|
|
||||||
return op;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int SkipNumber(string source, int i)
|
|
||||||
{
|
|
||||||
int length = source.Length;
|
|
||||||
if (source[i] == '0' && i + 1 < length && (source[i + 1] == 'x' || source[i + 1] == 'X'))
|
|
||||||
{
|
|
||||||
i += 2;
|
|
||||||
while (i < length && (IsHexDigit(source[i]) || source[i] == '.'))
|
|
||||||
i++;
|
|
||||||
if (i < length && (source[i] == 'p' || source[i] == 'P'))
|
|
||||||
{
|
|
||||||
i++;
|
|
||||||
if (i < length && (source[i] == '+' || source[i] == '-'))
|
|
||||||
i++;
|
|
||||||
while (i < length && IsDigit(source[i]))
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (i < length && IsDigit(source[i]))
|
|
||||||
i++;
|
|
||||||
if (i < length && source[i] == '.' && !(i + 1 < length && source[i + 1] == '.'))
|
|
||||||
{
|
|
||||||
i++;
|
|
||||||
while (i < length && IsDigit(source[i]))
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
if (i < length && (source[i] == 'e' || source[i] == 'E'))
|
|
||||||
{
|
|
||||||
i++;
|
|
||||||
if (i < length && (source[i] == '+' || source[i] == '-'))
|
|
||||||
i++;
|
|
||||||
while (i < length && IsDigit(source[i]))
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int SkipComment(string source, int i)
|
|
||||||
{
|
|
||||||
int level = LongBracketLevel(source, i);
|
|
||||||
if (level >= 0)
|
|
||||||
return SkipLongBracket(source, i, level);
|
|
||||||
|
|
||||||
int length = source.Length;
|
|
||||||
while (i < length && source[i] != '\n' && source[i] != '\r')
|
|
||||||
i++;
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int LongBracketLevel(string source, int i)
|
|
||||||
{
|
|
||||||
if (i >= source.Length || source[i] != '[')
|
|
||||||
return -1;
|
|
||||||
int j = i + 1;
|
|
||||||
int level = 0;
|
|
||||||
while (j < source.Length && source[j] == '=')
|
|
||||||
{
|
|
||||||
level++;
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
return j < source.Length && source[j] == '[' ? level : -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int SkipLongBracket(string source, int i, int level)
|
|
||||||
{
|
|
||||||
int length = source.Length;
|
|
||||||
i += level + 2;
|
|
||||||
while (i < length)
|
|
||||||
{
|
|
||||||
if (source[i] == ']')
|
|
||||||
{
|
|
||||||
int j = i + 1;
|
|
||||||
int count = 0;
|
|
||||||
while (j < length && source[j] == '=')
|
|
||||||
{
|
|
||||||
count++;
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
if (count == level && j < length && source[j] == ']')
|
|
||||||
return j + 1;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return length;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int SkipShortString(string source, int start)
|
|
||||||
{
|
|
||||||
char quote = source[start];
|
|
||||||
int i = start + 1;
|
|
||||||
int length = source.Length;
|
|
||||||
while (i < length)
|
|
||||||
{
|
|
||||||
char c = source[i];
|
|
||||||
if (c == '\\')
|
|
||||||
{
|
|
||||||
i += 2;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (c == quote)
|
|
||||||
return i + 1;
|
|
||||||
if (c == '\n' || c == '\r')
|
|
||||||
return i;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return length;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool IsNameStart(char c) => c == '_' || char.IsLetter(c);
|
|
||||||
|
|
||||||
private static bool IsNamePart(char c) => c == '_' || char.IsLetterOrDigit(c);
|
|
||||||
|
|
||||||
private static bool IsDigit(char c) => c >= '0' && c <= '9';
|
|
||||||
|
|
||||||
private static bool IsHexDigit(char c) => IsDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace LuaScript.Compat.Syntax
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
|
||||||
internal sealed class LuaSyntaxRuleAttribute(int order) : Attribute
|
|
||||||
{
|
|
||||||
public int Order { get; } = order;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
namespace LuaScript.Compat.Syntax
|
|
||||||
{
|
|
||||||
internal readonly struct LuaSyntaxToken
|
|
||||||
{
|
|
||||||
public LuaSyntaxToken(LuaSyntaxTokenKind kind, string text)
|
|
||||||
{
|
|
||||||
Kind = kind;
|
|
||||||
Text = text;
|
|
||||||
}
|
|
||||||
|
|
||||||
public LuaSyntaxTokenKind Kind { get; }
|
|
||||||
|
|
||||||
public string Text { get; }
|
|
||||||
|
|
||||||
public bool IsTrivia => Kind is LuaSyntaxTokenKind.Whitespace or LuaSyntaxTokenKind.Newline or LuaSyntaxTokenKind.Comment;
|
|
||||||
|
|
||||||
public bool IsName(string value) => Kind == LuaSyntaxTokenKind.Name && Text == value;
|
|
||||||
|
|
||||||
public bool IsOperator(string value) => Kind == LuaSyntaxTokenKind.Operator && Text == value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
namespace LuaScript.Compat.Syntax
|
|
||||||
{
|
|
||||||
internal enum LuaSyntaxTokenKind
|
|
||||||
{
|
|
||||||
Whitespace,
|
|
||||||
Newline,
|
|
||||||
Comment,
|
|
||||||
String,
|
|
||||||
InterpolatedString,
|
|
||||||
Number,
|
|
||||||
Name,
|
|
||||||
Operator,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
namespace LuaScript.Compat.Syntax.Rules
|
|
||||||
{
|
|
||||||
[LuaSyntaxRule(70)]
|
|
||||||
internal sealed class AugmentedAssignmentRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (context.Current.Kind != LuaSyntaxTokenKind.Operator)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
string? connective = Connective(context.Current.Text);
|
|
||||||
if (connective is null)
|
|
||||||
return false;
|
|
||||||
if (!context.TryReadTrailingLvalue(out _, out string target))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.Advance();
|
|
||||||
string value = context.ReadLineExpression();
|
|
||||||
string separator = context.LastOutputIsTrivia() ? string.Empty : " ";
|
|
||||||
context.EmitRaw($"{separator}= {target} {connective} ({value})");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string? Connective(string op) => op switch
|
|
||||||
{
|
|
||||||
"+=" => "+",
|
|
||||||
"-=" => "-",
|
|
||||||
"*=" => "*",
|
|
||||||
"/=" => "/",
|
|
||||||
"%=" => "%",
|
|
||||||
"^=" => "^",
|
|
||||||
"..=" => "..",
|
|
||||||
"||=" => "or",
|
|
||||||
"&&=" => "and",
|
|
||||||
_ => null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[LuaSyntaxRule(80)]
|
|
||||||
internal sealed class NilCoalesceAssignRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsOperator("??="))
|
|
||||||
return false;
|
|
||||||
if (!context.TryReadTrailingLvalue(out int start, out string target))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.RemoveOutputFrom(start);
|
|
||||||
context.Advance();
|
|
||||||
string value = context.ReadLineExpression();
|
|
||||||
context.EmitRaw($"if {target} == nil then {target} = ({value}) end");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[LuaSyntaxRule(90)]
|
|
||||||
internal sealed class IncrementRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsOperator("++"))
|
|
||||||
return false;
|
|
||||||
if (!context.TryReadTrailingLvalue(out _, out string target))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.Advance();
|
|
||||||
string separator = context.LastOutputIsTrivia() ? string.Empty : " ";
|
|
||||||
context.EmitRaw($"{separator}= {target} + 1");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
namespace LuaScript.Compat.Syntax.Rules
|
|
||||||
{
|
|
||||||
[LuaSyntaxRule(10)]
|
|
||||||
internal sealed class IsNotRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsName("is"))
|
|
||||||
return false;
|
|
||||||
if (!context.TryPreviousOutputSignificantSameLine(out var previous) || !LuaSyntaxFacts.IsValueEnd(previous))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int notIndex = context.NextSignificantSameLine(context.Index + 1);
|
|
||||||
if (notIndex < 0 || !context.Input[notIndex].IsName("not"))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int after = context.NextSignificant(notIndex + 1);
|
|
||||||
if (after < 0 || !LuaSyntaxFacts.IsValueStart(context.Input[after]))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.EmitOperator("~=");
|
|
||||||
context.Index = notIndex + 1;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[LuaSyntaxRule(20)]
|
|
||||||
internal sealed class IsRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsName("is"))
|
|
||||||
return false;
|
|
||||||
if (!context.TryPreviousOutputSignificantSameLine(out var previous) || !LuaSyntaxFacts.IsValueEnd(previous))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int next = context.NextSignificantSameLine(context.Index + 1);
|
|
||||||
if (next < 0 || !LuaSyntaxFacts.IsValueStart(context.Input[next]))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.EmitOperator("==");
|
|
||||||
context.Advance();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[LuaSyntaxRule(30)]
|
|
||||||
internal sealed class NotEqualRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsOperator("!="))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.EmitOperator("~=");
|
|
||||||
context.Advance();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace LuaScript.Compat.Syntax.Rules
|
|
||||||
{
|
|
||||||
[LuaSyntaxRule(5)]
|
|
||||||
internal sealed class LuaInterpolatedStringRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (context.Current.Kind != LuaSyntaxTokenKind.InterpolatedString)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
string text = context.Current.Text;
|
|
||||||
int end = text.Length;
|
|
||||||
if (end >= 2 && text[end - 1] == '`')
|
|
||||||
end--;
|
|
||||||
string content = text.Substring(1, end - 1);
|
|
||||||
|
|
||||||
var pieces = new List<string>();
|
|
||||||
var literal = new StringBuilder();
|
|
||||||
int i = 0;
|
|
||||||
while (i < content.Length)
|
|
||||||
{
|
|
||||||
char c = content[i];
|
|
||||||
if (c == '\\' && i + 1 < content.Length)
|
|
||||||
{
|
|
||||||
literal.Append(content[i]);
|
|
||||||
literal.Append(content[i + 1]);
|
|
||||||
i += 2;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (c == '{')
|
|
||||||
{
|
|
||||||
int close = FindClosingBrace(content, i + 1);
|
|
||||||
if (close < 0)
|
|
||||||
{
|
|
||||||
literal.Append(c);
|
|
||||||
i++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
string expression = content.Substring(i + 1, close - i - 1).Trim();
|
|
||||||
if (expression.Length > 0)
|
|
||||||
{
|
|
||||||
FlushLiteral(pieces, literal);
|
|
||||||
pieces.Add($"tostring({LuaSyntaxExtensions.Rewrite(expression)})");
|
|
||||||
}
|
|
||||||
i = close + 1;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
literal.Append(c);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
FlushLiteral(pieces, literal);
|
|
||||||
|
|
||||||
context.Advance();
|
|
||||||
if (pieces.Count == 0)
|
|
||||||
{
|
|
||||||
context.EmitRaw("\"\"");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (pieces.Count == 1)
|
|
||||||
{
|
|
||||||
context.EmitRaw(pieces[0]);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
context.EmitRaw($"({string.Join(" .. ", pieces)})");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void FlushLiteral(List<string> pieces, StringBuilder literal)
|
|
||||||
{
|
|
||||||
if (literal.Length == 0)
|
|
||||||
return;
|
|
||||||
pieces.Add($"\"{literal.ToString().Replace("\"", "\\\"")}\"");
|
|
||||||
literal.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int FindClosingBrace(string content, int from)
|
|
||||||
{
|
|
||||||
int depth = 0;
|
|
||||||
char quote = '\0';
|
|
||||||
for (int i = from; i < content.Length; i++)
|
|
||||||
{
|
|
||||||
char c = content[i];
|
|
||||||
if (quote != '\0')
|
|
||||||
{
|
|
||||||
if (c == '\\')
|
|
||||||
{
|
|
||||||
i++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (c == quote)
|
|
||||||
quote = '\0';
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (c == '"' || c == '\'')
|
|
||||||
{
|
|
||||||
quote = c;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (c == '{')
|
|
||||||
{
|
|
||||||
depth++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (c == '}')
|
|
||||||
{
|
|
||||||
if (depth == 0)
|
|
||||||
return i;
|
|
||||||
depth--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
namespace LuaScript.Compat.Syntax.Rules
|
|
||||||
{
|
|
||||||
[LuaSyntaxRule(35)]
|
|
||||||
internal sealed class LuaLambdaRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsOperator("=>"))
|
|
||||||
return false;
|
|
||||||
if (!context.TryReadTrailingLvalue(out int start, out string target))
|
|
||||||
return false;
|
|
||||||
if (!TryNormalizeParameters(target, out string parameters))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int body = context.NextSignificant(context.Index + 1);
|
|
||||||
if (body < 0 || !LuaSyntaxFacts.IsValueStart(context.Input[body]))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.RemoveOutputFrom(start);
|
|
||||||
context.Advance();
|
|
||||||
string expression = context.ReadForwardOrOperand();
|
|
||||||
if (expression.Length == 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.EmitRaw($"(function({parameters}) return {LuaSyntaxExtensions.Rewrite(expression)} end)");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool TryNormalizeParameters(string text, out string parameters)
|
|
||||||
{
|
|
||||||
parameters = string.Empty;
|
|
||||||
text = text.Trim();
|
|
||||||
bool wrapped = text.StartsWith("(") && text.EndsWith(")");
|
|
||||||
if (wrapped)
|
|
||||||
text = text.Substring(1, text.Length - 2).Trim();
|
|
||||||
|
|
||||||
if (text.Length == 0)
|
|
||||||
return wrapped;
|
|
||||||
|
|
||||||
var names = text.Split(',');
|
|
||||||
for (int i = 0; i < names.Length; i++)
|
|
||||||
{
|
|
||||||
string name = names[i].Trim();
|
|
||||||
if (name == "...")
|
|
||||||
{
|
|
||||||
if (i != names.Length - 1)
|
|
||||||
return false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!IsName(name) || LuaKeywords.IsReserved(name))
|
|
||||||
return false;
|
|
||||||
if (!wrapped && names.Length > 1)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
parameters = string.Join(", ", System.Array.ConvertAll(names, static n => n.Trim()));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool IsName(string text)
|
|
||||||
{
|
|
||||||
if (text.Length == 0)
|
|
||||||
return false;
|
|
||||||
if (text[0] != '_' && !char.IsLetter(text[0]))
|
|
||||||
return false;
|
|
||||||
for (int i = 1; i < text.Length; i++)
|
|
||||||
{
|
|
||||||
if (text[i] != '_' && !char.IsLetterOrDigit(text[i]))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
namespace LuaScript.Compat.Syntax.Rules
|
|
||||||
{
|
|
||||||
[LuaSyntaxRule(40)]
|
|
||||||
internal sealed class LogicalNotRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsOperator("!"))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.Advance();
|
|
||||||
context.EmitWord("not");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[LuaSyntaxRule(50)]
|
|
||||||
internal sealed class AndAliasRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsOperator("&&"))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.Advance();
|
|
||||||
context.EmitWord("and");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[LuaSyntaxRule(60)]
|
|
||||||
internal sealed class OrAliasRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsOperator("||"))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.Advance();
|
|
||||||
context.EmitWord("or");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
namespace LuaScript.Compat.Syntax.Rules
|
|
||||||
{
|
|
||||||
[LuaSyntaxRule(100)]
|
|
||||||
internal sealed class NilCoalesceRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsOperator("??"))
|
|
||||||
return false;
|
|
||||||
if (!context.TryPopOrOperand(out string left))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.Advance();
|
|
||||||
string right = context.ReadForwardOrOperand();
|
|
||||||
string temporary = context.NextTemporary();
|
|
||||||
context.EmitRaw($"(function() local {temporary} = ({left}) if {temporary} ~= nil then return {temporary} else return ({right}) end end)()");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
namespace LuaScript.Compat.Syntax.Rules
|
|
||||||
{
|
|
||||||
[LuaSyntaxRule(110)]
|
|
||||||
internal sealed class LuaNumericRangeRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsName("in"))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int open = context.NextSignificant(context.Index + 1);
|
|
||||||
if (open < 0 || !context.Input[open].IsOperator("<"))
|
|
||||||
return false;
|
|
||||||
if (!LuaRangeSyntax.TryParse(context.Input, open, out var parts, out int end))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
string stopExpression = parts.Exclusive ? $"({parts.Stop}) - 1" : parts.Stop;
|
|
||||||
string replacement = parts.Step is null
|
|
||||||
? $"= {parts.Start}, {stopExpression}"
|
|
||||||
: $"= {parts.Start}, {stopExpression}, {parts.Step}";
|
|
||||||
context.EmitRaw(replacement);
|
|
||||||
context.Index = end;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,122 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace LuaScript.Compat.Syntax.Rules
|
|
||||||
{
|
|
||||||
[LuaSyntaxRule(98)]
|
|
||||||
internal sealed class LuaPipeRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsOperator("|>"))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int targetStart = context.NextSignificant(context.Index + 1);
|
|
||||||
if (targetStart < 0 || !LuaSyntaxFacts.IsValueStart(context.Input[targetStart]))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!context.TryPopOrOperand(out string left))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.Advance();
|
|
||||||
var segment = ReadSegment(context);
|
|
||||||
string call = BuildCall(segment, left);
|
|
||||||
context.EmitRaw(call);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<LuaSyntaxToken> ReadSegment(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
var tokens = new List<LuaSyntaxToken>();
|
|
||||||
int depth = 0;
|
|
||||||
while (!context.AtEnd)
|
|
||||||
{
|
|
||||||
var token = context.Current;
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Newline && depth == 0)
|
|
||||||
break;
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Name && depth == 0 && LuaKeywords.IsExpressionBoundary(token.Text))
|
|
||||||
break;
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Operator)
|
|
||||||
{
|
|
||||||
if (token.Text is "(" or "[" or "{")
|
|
||||||
{
|
|
||||||
depth++;
|
|
||||||
}
|
|
||||||
else if (token.Text is ")" or "]" or "}")
|
|
||||||
{
|
|
||||||
if (depth == 0)
|
|
||||||
break;
|
|
||||||
depth--;
|
|
||||||
}
|
|
||||||
else if (depth == 0 && token.Text is "," or ";" or "|>")
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tokens.Add(token);
|
|
||||||
context.Advance();
|
|
||||||
}
|
|
||||||
while (tokens.Count > 0 && tokens[0].IsTrivia)
|
|
||||||
tokens.RemoveAt(0);
|
|
||||||
while (tokens.Count > 0 && tokens[tokens.Count - 1].IsTrivia)
|
|
||||||
tokens.RemoveAt(tokens.Count - 1);
|
|
||||||
return tokens;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string BuildCall(List<LuaSyntaxToken> segment, string left)
|
|
||||||
{
|
|
||||||
string argument = $"({left})";
|
|
||||||
if (segment.Count == 0)
|
|
||||||
return argument;
|
|
||||||
|
|
||||||
if (segment[segment.Count - 1].IsOperator(")"))
|
|
||||||
{
|
|
||||||
int open = FindOpeningParen(segment);
|
|
||||||
if (open > 0)
|
|
||||||
{
|
|
||||||
string callee = Concat(segment, 0, open);
|
|
||||||
string arguments = Concat(segment, open + 1, segment.Count - 1);
|
|
||||||
return arguments.Length == 0
|
|
||||||
? $"{callee}({argument})"
|
|
||||||
: $"{callee}({argument}, {arguments})";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $"{Concat(segment, 0, segment.Count)}({argument})";
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int FindOpeningParen(List<LuaSyntaxToken> segment)
|
|
||||||
{
|
|
||||||
int depth = 0;
|
|
||||||
for (int i = segment.Count - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
var token = segment[i];
|
|
||||||
if (token.Kind != LuaSyntaxTokenKind.Operator)
|
|
||||||
continue;
|
|
||||||
if (token.Text is ")" or "]" or "}")
|
|
||||||
{
|
|
||||||
depth++;
|
|
||||||
}
|
|
||||||
else if (token.Text is "(" or "[" or "{")
|
|
||||||
{
|
|
||||||
depth--;
|
|
||||||
if (depth == 0)
|
|
||||||
return token.Text == "(" ? i : -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string Concat(List<LuaSyntaxToken> segment, int begin, int end)
|
|
||||||
{
|
|
||||||
while (begin < end && segment[begin].IsTrivia)
|
|
||||||
begin++;
|
|
||||||
while (end > begin && segment[end - 1].IsTrivia)
|
|
||||||
end--;
|
|
||||||
var builder = new StringBuilder();
|
|
||||||
for (int i = begin; i < end; i++)
|
|
||||||
builder.Append(segment[i].Text);
|
|
||||||
return builder.ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
namespace LuaScript.Compat.Syntax.Rules
|
|
||||||
{
|
|
||||||
[LuaSyntaxRule(108)]
|
|
||||||
internal sealed class LuaRangeMembershipRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsName("in"))
|
|
||||||
return false;
|
|
||||||
if (IsForHeader(context))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int open = context.NextSignificant(context.Index + 1);
|
|
||||||
if (open < 0 || !context.Input[open].IsOperator("<"))
|
|
||||||
return false;
|
|
||||||
if (!LuaRangeSyntax.TryParse(context.Input, open, out var parts, out int end))
|
|
||||||
return false;
|
|
||||||
if (parts.Step is not null)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!context.TryPopOrOperand(out string left))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
string temporary = context.NextTemporary();
|
|
||||||
string upper = parts.Exclusive ? "<" : "<=";
|
|
||||||
context.EmitRaw($"(function() local {temporary} = ({left}) return {temporary} >= ({LuaSyntaxExtensions.Rewrite(parts.Start)}) and {temporary} {upper} ({LuaSyntaxExtensions.Rewrite(parts.Stop)}) end)()");
|
|
||||||
context.Index = end;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool IsForHeader(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
var output = context.Output;
|
|
||||||
for (int i = output.Count - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
var token = output[i];
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Newline)
|
|
||||||
return false;
|
|
||||||
if (token.Kind != LuaSyntaxTokenKind.Name)
|
|
||||||
continue;
|
|
||||||
if (token.Text is "do" or "then")
|
|
||||||
return false;
|
|
||||||
if (token.Text == "for")
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace LuaScript.Compat.Syntax.Rules
|
|
||||||
{
|
|
||||||
[LuaSyntaxRule(92)]
|
|
||||||
internal sealed class LuaSafeNavigationRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsOperator("?"))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int accessor = context.NextSignificantSameLine(context.Index + 1);
|
|
||||||
if (accessor < 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (context.Input[accessor].IsOperator("."))
|
|
||||||
return TryApplyMember(context, accessor);
|
|
||||||
if (context.Input[accessor].IsOperator("["))
|
|
||||||
return TryApplyIndex(context, accessor);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool TryApplyMember(LuaRewriteContext context, int accessor)
|
|
||||||
{
|
|
||||||
int name = context.NextSignificantSameLine(accessor + 1);
|
|
||||||
if (name < 0)
|
|
||||||
return false;
|
|
||||||
var token = context.Input[name];
|
|
||||||
if (token.Kind != LuaSyntaxTokenKind.Name || LuaKeywords.IsReserved(token.Text))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!context.TryReadTrailingLvalue(out int start, out string target))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.RemoveOutputFrom(start);
|
|
||||||
context.Index = name + 1;
|
|
||||||
string temporary = context.NextTemporary();
|
|
||||||
context.EmitRaw($"(function() local {temporary} = ({target}) if {temporary} == nil then return nil end return {temporary}.{token.Text} end)()");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool TryApplyIndex(LuaRewriteContext context, int accessor)
|
|
||||||
{
|
|
||||||
int close = FindClosingBracket(context, accessor + 1);
|
|
||||||
if (close < 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
string key = Concat(context, accessor + 1, close);
|
|
||||||
if (key.Length == 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!context.TryReadTrailingLvalue(out int start, out string target))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.RemoveOutputFrom(start);
|
|
||||||
context.Index = close + 1;
|
|
||||||
string temporary = context.NextTemporary();
|
|
||||||
context.EmitRaw($"(function() local {temporary} = ({target}) if {temporary} == nil then return nil end return {temporary}[{LuaSyntaxExtensions.Rewrite(key)}] end)()");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int FindClosingBracket(LuaRewriteContext context, int from)
|
|
||||||
{
|
|
||||||
int depth = 0;
|
|
||||||
for (int i = from; i < context.Input.Count; i++)
|
|
||||||
{
|
|
||||||
var token = context.Input[i];
|
|
||||||
if (token.Kind != LuaSyntaxTokenKind.Operator)
|
|
||||||
continue;
|
|
||||||
if (token.Text is "(" or "[" or "{")
|
|
||||||
{
|
|
||||||
depth++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (token.Text is ")" or "}")
|
|
||||||
{
|
|
||||||
if (depth == 0)
|
|
||||||
return -1;
|
|
||||||
depth--;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (token.Text == "]")
|
|
||||||
{
|
|
||||||
if (depth == 0)
|
|
||||||
return i;
|
|
||||||
depth--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string Concat(LuaRewriteContext context, int begin, int end)
|
|
||||||
{
|
|
||||||
while (begin < end && context.Input[begin].IsTrivia)
|
|
||||||
begin++;
|
|
||||||
while (end > begin && context.Input[end - 1].IsTrivia)
|
|
||||||
end--;
|
|
||||||
var builder = new StringBuilder();
|
|
||||||
for (int i = begin; i < end; i++)
|
|
||||||
builder.Append(context.Input[i].Text);
|
|
||||||
return builder.ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
namespace LuaScript.Compat.Syntax.Rules
|
|
||||||
{
|
|
||||||
[LuaSyntaxRule(96)]
|
|
||||||
internal sealed class LuaTernaryRule : ILuaSyntaxRule
|
|
||||||
{
|
|
||||||
public bool TryApply(LuaRewriteContext context)
|
|
||||||
{
|
|
||||||
if (!context.Current.IsOperator("?"))
|
|
||||||
return false;
|
|
||||||
if (!context.TryPreviousOutputSignificantSameLine(out var previous) || !LuaSyntaxFacts.IsValueEnd(previous))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int thenStart = context.NextSignificantSameLine(context.Index + 1);
|
|
||||||
if (thenStart < 0 || !LuaSyntaxFacts.IsValueStart(context.Input[thenStart]))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int colon = FindColon(context, context.Index + 1);
|
|
||||||
if (colon < 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int elseStart = context.NextSignificantSameLine(colon + 1);
|
|
||||||
if (elseStart < 0 || !LuaSyntaxFacts.IsValueStart(context.Input[elseStart]))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!context.TryPopOrOperand(out string condition))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
context.Index++;
|
|
||||||
string thenExpression = ReadUntil(context, colon);
|
|
||||||
context.Index = colon + 1;
|
|
||||||
string elseExpression = context.ReadForwardOrOperand();
|
|
||||||
|
|
||||||
context.EmitRaw($"(function() if {condition} then return ({LuaSyntaxExtensions.Rewrite(thenExpression)}) else return ({LuaSyntaxExtensions.Rewrite(elseExpression)}) end end)()");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int FindColon(LuaRewriteContext context, int from)
|
|
||||||
{
|
|
||||||
int depth = 0;
|
|
||||||
for (int i = from; i < context.Input.Count; i++)
|
|
||||||
{
|
|
||||||
var token = context.Input[i];
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Newline)
|
|
||||||
return -1;
|
|
||||||
if (token.Kind == LuaSyntaxTokenKind.Name && depth == 0 && LuaKeywords.IsExpressionBoundary(token.Text))
|
|
||||||
return -1;
|
|
||||||
if (token.Kind != LuaSyntaxTokenKind.Operator)
|
|
||||||
continue;
|
|
||||||
if (token.Text is "(" or "[" or "{")
|
|
||||||
{
|
|
||||||
depth++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (token.Text is ")" or "]" or "}")
|
|
||||||
{
|
|
||||||
if (depth == 0)
|
|
||||||
return -1;
|
|
||||||
depth--;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (depth > 0)
|
|
||||||
continue;
|
|
||||||
if (token.Text is "," or ";" or "?")
|
|
||||||
return -1;
|
|
||||||
if (token.Text == ":")
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string ReadUntil(LuaRewriteContext context, int stop)
|
|
||||||
{
|
|
||||||
var builder = new System.Text.StringBuilder();
|
|
||||||
int begin = context.Index;
|
|
||||||
int end = stop;
|
|
||||||
while (begin < end && context.Input[begin].IsTrivia)
|
|
||||||
begin++;
|
|
||||||
while (end > begin && context.Input[end - 1].IsTrivia)
|
|
||||||
end--;
|
|
||||||
for (int i = begin; i < end; i++)
|
|
||||||
builder.Append(context.Input[i].Text);
|
|
||||||
return builder.ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
using System.Collections;
|
|
||||||
|
|
||||||
namespace LuaScript.Generator
|
|
||||||
{
|
|
||||||
internal readonly struct EquatableArray<T>(T[] items) : IEquatable<EquatableArray<T>>, IReadOnlyList<T>
|
|
||||||
where T : IEquatable<T>
|
|
||||||
{
|
|
||||||
private readonly T[] _items = items;
|
|
||||||
|
|
||||||
public static readonly EquatableArray<T> Empty = new([]);
|
|
||||||
|
|
||||||
public int Count => _items?.Length ?? 0;
|
|
||||||
|
|
||||||
public T this[int index] => _items[index];
|
|
||||||
|
|
||||||
public bool Equals(EquatableArray<T> other)
|
|
||||||
{
|
|
||||||
var a = _items ?? [];
|
|
||||||
var b = other._items ?? [];
|
|
||||||
if (a.Length != b.Length)
|
|
||||||
return false;
|
|
||||||
for (int i = 0; i < a.Length; i++)
|
|
||||||
{
|
|
||||||
if (!a[i].Equals(b[i]))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool Equals(object? obj) => obj is EquatableArray<T> other && Equals(other);
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
var items = _items ?? [];
|
|
||||||
int hash = 17;
|
|
||||||
foreach (var item in items)
|
|
||||||
hash = hash * 31 + (item?.GetHashCode() ?? 0);
|
|
||||||
return hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>)(_items ?? [])).GetEnumerator();
|
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace System.Runtime.CompilerServices
|
|
||||||
{
|
|
||||||
internal static class IsExternalInit;
|
|
||||||
}
|
|
||||||
@@ -1,486 +0,0 @@
|
|||||||
using System.Text;
|
|
||||||
using Microsoft.CodeAnalysis;
|
|
||||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
||||||
using Microsoft.CodeAnalysis.Text;
|
|
||||||
|
|
||||||
namespace LuaScript.Generator
|
|
||||||
{
|
|
||||||
internal enum LuaParameterKind
|
|
||||||
{
|
|
||||||
ExecutionContext,
|
|
||||||
Arguments,
|
|
||||||
DynValue,
|
|
||||||
Double,
|
|
||||||
Int,
|
|
||||||
Bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
internal enum LuaReturnKind
|
|
||||||
{
|
|
||||||
DynValue,
|
|
||||||
Double,
|
|
||||||
Void,
|
|
||||||
}
|
|
||||||
|
|
||||||
internal sealed record LuaParameterModel(string Name, LuaParameterKind Kind, string DefaultLiteral);
|
|
||||||
|
|
||||||
internal sealed record LuaFunctionModel(
|
|
||||||
string LuaName,
|
|
||||||
string MethodName,
|
|
||||||
LuaReturnKind ReturnKind,
|
|
||||||
EquatableArray<LuaParameterModel> Parameters);
|
|
||||||
|
|
||||||
internal sealed record LuaConstantModel(string LuaName, string MemberName);
|
|
||||||
|
|
||||||
internal enum LuaUpdateKind
|
|
||||||
{
|
|
||||||
Direct,
|
|
||||||
OptionalValue,
|
|
||||||
OptionalReference,
|
|
||||||
}
|
|
||||||
|
|
||||||
internal sealed record LuaUpdateModel(string LuaName, string MethodName, LuaUpdateKind Kind);
|
|
||||||
|
|
||||||
internal sealed record LuaCatalogEntry(
|
|
||||||
string Table,
|
|
||||||
string Name,
|
|
||||||
bool IsFunction,
|
|
||||||
EquatableArray<string> Parameters);
|
|
||||||
|
|
||||||
internal sealed record LuaTypePart(string Name, bool IsStatic);
|
|
||||||
|
|
||||||
internal sealed record LuaTableModel(
|
|
||||||
string Namespace,
|
|
||||||
EquatableArray<LuaTypePart> TypeChain,
|
|
||||||
string TableName,
|
|
||||||
string ContextType,
|
|
||||||
EquatableArray<LuaFunctionModel> Functions,
|
|
||||||
EquatableArray<LuaConstantModel> Constants,
|
|
||||||
EquatableArray<LuaUpdateModel> Updates,
|
|
||||||
EquatableArray<LuaCatalogEntry> Entries);
|
|
||||||
|
|
||||||
[Generator]
|
|
||||||
public sealed class LuaApiGenerator : IIncrementalGenerator
|
|
||||||
{
|
|
||||||
private const string TableAttributeName = "LuaScript.Api.LuaTableAttribute";
|
|
||||||
private const string FunctionAttributeName = "LuaScript.Api.LuaFunctionAttribute";
|
|
||||||
private const string ConstantAttributeName = "LuaScript.Api.LuaConstantAttribute";
|
|
||||||
private const string VariableAttributeName = "LuaScript.Api.LuaVariableAttribute";
|
|
||||||
private const string BuiltinAttributeName = "LuaScript.Api.LuaBuiltinAttribute";
|
|
||||||
|
|
||||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
|
||||||
{
|
|
||||||
var tables = context.SyntaxProvider
|
|
||||||
.ForAttributeWithMetadataName(
|
|
||||||
TableAttributeName,
|
|
||||||
static (node, _) => node is ClassDeclarationSyntax,
|
|
||||||
static (ctx, _) => CreateModel(ctx))
|
|
||||||
.Where(static model => model is not null)
|
|
||||||
.Select(static (model, _) => model!);
|
|
||||||
|
|
||||||
var assemblyEntries = context.CompilationProvider
|
|
||||||
.Select(static (compilation, _) => CollectAssemblyEntries(compilation));
|
|
||||||
|
|
||||||
context.RegisterSourceOutput(tables, static (ctx, model) =>
|
|
||||||
{
|
|
||||||
if (model.Functions.Count == 0 && model.Constants.Count == 0 && model.Updates.Count == 0)
|
|
||||||
return;
|
|
||||||
ctx.AddSource(RegistrationHintName(model), SourceText.From(EmitRegistration(model), Encoding.UTF8));
|
|
||||||
});
|
|
||||||
|
|
||||||
var catalog = tables.Collect().Combine(assemblyEntries);
|
|
||||||
context.RegisterSourceOutput(catalog, static (ctx, source) =>
|
|
||||||
{
|
|
||||||
var (models, extra) = source;
|
|
||||||
var entries = models
|
|
||||||
.OrderBy(static m => m.Namespace, StringComparer.Ordinal)
|
|
||||||
.ThenBy(static m => string.Join("+", m.TypeChain.Select(static p => p.Name)), StringComparer.Ordinal)
|
|
||||||
.SelectMany(static m => m.Entries)
|
|
||||||
.Concat(extra)
|
|
||||||
.ToArray();
|
|
||||||
if (entries.Length == 0)
|
|
||||||
return;
|
|
||||||
ctx.AddSource("LuaApiCatalog.g.cs", SourceText.From(EmitCatalog(entries), Encoding.UTF8));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static LuaTableModel? CreateModel(GeneratorAttributeSyntaxContext context)
|
|
||||||
{
|
|
||||||
if (context.TargetSymbol is not INamedTypeSymbol symbol)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var tableArguments = context.Attributes[0].ConstructorArguments;
|
|
||||||
string tableName = tableArguments.Length > 0 && tableArguments[0].Value is string name ? name : string.Empty;
|
|
||||||
|
|
||||||
var chain = new List<LuaTypePart>();
|
|
||||||
for (var type = symbol; type is not null; type = type.ContainingType)
|
|
||||||
chain.Insert(0, new LuaTypePart(type.Name, type.IsStatic));
|
|
||||||
|
|
||||||
var entries = new List<(int Position, LuaCatalogEntry Entry)>();
|
|
||||||
foreach (var attribute in symbol.GetAttributes())
|
|
||||||
{
|
|
||||||
string? attributeName = attribute.AttributeClass?.ToDisplayString();
|
|
||||||
if (attributeName is not (VariableAttributeName or BuiltinAttributeName or FunctionAttributeName))
|
|
||||||
continue;
|
|
||||||
var entry = CreateAttributeEntry(attribute, tableName);
|
|
||||||
if (entry is null)
|
|
||||||
continue;
|
|
||||||
int position = attribute.ApplicationSyntaxReference?.Span.Start ?? 0;
|
|
||||||
entries.Add((position, entry));
|
|
||||||
}
|
|
||||||
var classEntries = entries.OrderBy(static e => e.Position).Select(static e => e.Entry).ToList();
|
|
||||||
|
|
||||||
var functions = new List<(int Position, LuaFunctionModel Function, LuaCatalogEntry Entry)>();
|
|
||||||
var constants = new List<(int Position, LuaConstantModel Constant, LuaCatalogEntry Entry)>();
|
|
||||||
var updates = new List<(int Position, LuaUpdateModel Update, LuaCatalogEntry? Entry)>();
|
|
||||||
string contextType = string.Empty;
|
|
||||||
foreach (var member in symbol.GetMembers())
|
|
||||||
{
|
|
||||||
int position = member.DeclaringSyntaxReferences.Length > 0
|
|
||||||
? member.DeclaringSyntaxReferences[0].Span.Start
|
|
||||||
: 0;
|
|
||||||
|
|
||||||
if (member is IMethodSymbol method)
|
|
||||||
{
|
|
||||||
var functionAttribute = FindAttribute(method, FunctionAttributeName);
|
|
||||||
if (functionAttribute is not null)
|
|
||||||
{
|
|
||||||
var function = CreateFunctionModel(method, functionAttribute, out var catalogParameters);
|
|
||||||
if (function is null)
|
|
||||||
continue;
|
|
||||||
functions.Add((position, function, new LuaCatalogEntry(tableName, function.LuaName, true, catalogParameters)));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var variableAttribute = FindAttribute(method, VariableAttributeName);
|
|
||||||
if (variableAttribute is not null)
|
|
||||||
{
|
|
||||||
var update = CreateUpdateModel(method, variableAttribute, tableName, ref contextType, out var entry);
|
|
||||||
if (update is not null)
|
|
||||||
updates.Add((position, update, entry));
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (member is IFieldSymbol or IPropertySymbol)
|
|
||||||
{
|
|
||||||
var attribute = FindAttribute(member, ConstantAttributeName);
|
|
||||||
if (attribute is null)
|
|
||||||
continue;
|
|
||||||
if (attribute.ConstructorArguments.Length == 0 || attribute.ConstructorArguments[0].Value is not string constantName)
|
|
||||||
continue;
|
|
||||||
constants.Add((
|
|
||||||
position,
|
|
||||||
new LuaConstantModel(constantName, member.Name),
|
|
||||||
new LuaCatalogEntry(tableName, constantName, false, EquatableArray<string>.Empty)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var orderedMembers = functions
|
|
||||||
.Select(static f => (Position: f.Position, Entry: f.Entry))
|
|
||||||
.Concat(constants.Select(static c => (Position: c.Position, Entry: c.Entry)))
|
|
||||||
.Concat(updates.Where(static u => u.Entry is not null).Select(static u => (Position: u.Position, Entry: u.Entry!)))
|
|
||||||
.OrderBy(static e => e.Position)
|
|
||||||
.Select(static e => e.Entry);
|
|
||||||
|
|
||||||
return new LuaTableModel(
|
|
||||||
symbol.ContainingNamespace.ToDisplayString(),
|
|
||||||
new EquatableArray<LuaTypePart>([.. chain]),
|
|
||||||
tableName,
|
|
||||||
contextType,
|
|
||||||
new EquatableArray<LuaFunctionModel>([.. functions.OrderBy(static f => f.Position).Select(static f => f.Function)]),
|
|
||||||
new EquatableArray<LuaConstantModel>([.. constants.OrderBy(static c => c.Position).Select(static c => c.Constant)]),
|
|
||||||
new EquatableArray<LuaUpdateModel>([.. updates.OrderBy(static u => u.Position).Select(static u => u.Update)]),
|
|
||||||
new EquatableArray<LuaCatalogEntry>([.. classEntries, .. orderedMembers]));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static AttributeData? FindAttribute(ISymbol symbol, string attributeName)
|
|
||||||
{
|
|
||||||
foreach (var attribute in symbol.GetAttributes())
|
|
||||||
{
|
|
||||||
if (attribute.AttributeClass?.ToDisplayString() == attributeName)
|
|
||||||
return attribute;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static LuaCatalogEntry? CreateAttributeEntry(AttributeData attribute, string defaultTable)
|
|
||||||
{
|
|
||||||
if (attribute.ConstructorArguments.Length == 0 || attribute.ConstructorArguments[0].Value is not string name)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
string table = defaultTable;
|
|
||||||
foreach (var argument in attribute.NamedArguments)
|
|
||||||
{
|
|
||||||
if (argument.Key == "Table" && argument.Value.Value is string overridden)
|
|
||||||
table = overridden;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isFunction = attribute.AttributeClass?.ToDisplayString() is (BuiltinAttributeName or FunctionAttributeName);
|
|
||||||
var parameters = isFunction && attribute.ConstructorArguments.Length > 1
|
|
||||||
? ExtractStringArray(attribute.ConstructorArguments[1])
|
|
||||||
: EquatableArray<string>.Empty;
|
|
||||||
|
|
||||||
return new LuaCatalogEntry(table, name, isFunction, parameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static EquatableArray<string> ExtractStringArray(TypedConstant constant)
|
|
||||||
{
|
|
||||||
if (constant.Kind != TypedConstantKind.Array)
|
|
||||||
return EquatableArray<string>.Empty;
|
|
||||||
return new EquatableArray<string>([.. constant.Values.Select(static v => v.Value as string ?? string.Empty)]);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static LuaFunctionModel? CreateFunctionModel(IMethodSymbol method, AttributeData attribute, out EquatableArray<string> catalogParameters)
|
|
||||||
{
|
|
||||||
catalogParameters = EquatableArray<string>.Empty;
|
|
||||||
if (attribute.ConstructorArguments.Length == 0 || attribute.ConstructorArguments[0].Value is not string luaName)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
LuaReturnKind returnKind;
|
|
||||||
if (method.ReturnsVoid)
|
|
||||||
returnKind = LuaReturnKind.Void;
|
|
||||||
else if (method.ReturnType.ToDisplayString() == "MoonSharp.Interpreter.DynValue")
|
|
||||||
returnKind = LuaReturnKind.DynValue;
|
|
||||||
else if (method.ReturnType.SpecialType == SpecialType.System_Double)
|
|
||||||
returnKind = LuaReturnKind.Double;
|
|
||||||
else
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var parameters = new List<LuaParameterModel>();
|
|
||||||
foreach (var parameter in method.Parameters)
|
|
||||||
{
|
|
||||||
LuaParameterKind kind;
|
|
||||||
string defaultLiteral = string.Empty;
|
|
||||||
switch (parameter.Type.ToDisplayString())
|
|
||||||
{
|
|
||||||
case "MoonSharp.Interpreter.ScriptExecutionContext":
|
|
||||||
kind = LuaParameterKind.ExecutionContext;
|
|
||||||
break;
|
|
||||||
case "MoonSharp.Interpreter.CallbackArguments":
|
|
||||||
kind = LuaParameterKind.Arguments;
|
|
||||||
break;
|
|
||||||
case "MoonSharp.Interpreter.DynValue":
|
|
||||||
kind = LuaParameterKind.DynValue;
|
|
||||||
break;
|
|
||||||
case "double":
|
|
||||||
kind = LuaParameterKind.Double;
|
|
||||||
defaultLiteral = FormatDouble(parameter.HasExplicitDefaultValue ? (double)parameter.ExplicitDefaultValue! : 0d);
|
|
||||||
break;
|
|
||||||
case "int":
|
|
||||||
kind = LuaParameterKind.Int;
|
|
||||||
defaultLiteral = FormatDouble(parameter.HasExplicitDefaultValue ? (int)parameter.ExplicitDefaultValue! : 0);
|
|
||||||
break;
|
|
||||||
case "bool":
|
|
||||||
kind = LuaParameterKind.Bool;
|
|
||||||
defaultLiteral = parameter.HasExplicitDefaultValue && (bool)parameter.ExplicitDefaultValue! ? "true" : "false";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
parameters.Add(new LuaParameterModel(parameter.Name, kind, defaultLiteral));
|
|
||||||
}
|
|
||||||
|
|
||||||
var explicitParameters = attribute.ConstructorArguments.Length > 1
|
|
||||||
? ExtractStringArray(attribute.ConstructorArguments[1])
|
|
||||||
: EquatableArray<string>.Empty;
|
|
||||||
catalogParameters = explicitParameters.Count > 0
|
|
||||||
? explicitParameters
|
|
||||||
: new EquatableArray<string>([.. parameters
|
|
||||||
.Where(static p => p.Kind is not (LuaParameterKind.ExecutionContext or LuaParameterKind.Arguments))
|
|
||||||
.Select(static p => p.Name)]);
|
|
||||||
|
|
||||||
return new LuaFunctionModel(luaName, method.Name, returnKind, new EquatableArray<LuaParameterModel>([.. parameters]));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static LuaUpdateModel? CreateUpdateModel(IMethodSymbol method, AttributeData attribute, string tableName, ref string contextType, out LuaCatalogEntry? entry)
|
|
||||||
{
|
|
||||||
entry = null;
|
|
||||||
if (attribute.ConstructorArguments.Length == 0 || attribute.ConstructorArguments[0].Value is not string luaName)
|
|
||||||
return null;
|
|
||||||
if (method.Parameters.Length != 1)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
string parameterType = method.Parameters[0].Type.ToDisplayString();
|
|
||||||
if (contextType.Length == 0)
|
|
||||||
contextType = parameterType;
|
|
||||||
else if (contextType != parameterType)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var returnType = method.ReturnType;
|
|
||||||
LuaUpdateKind kind;
|
|
||||||
if (returnType.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
|
|
||||||
kind = LuaUpdateKind.OptionalValue;
|
|
||||||
else if (returnType.IsReferenceType)
|
|
||||||
kind = LuaUpdateKind.OptionalReference;
|
|
||||||
else
|
|
||||||
kind = LuaUpdateKind.Direct;
|
|
||||||
|
|
||||||
bool inCatalog = true;
|
|
||||||
foreach (var argument in attribute.NamedArguments)
|
|
||||||
{
|
|
||||||
if (argument.Key == "InCatalog" && argument.Value.Value is bool value)
|
|
||||||
inCatalog = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inCatalog)
|
|
||||||
entry = new LuaCatalogEntry(tableName, luaName, false, EquatableArray<string>.Empty);
|
|
||||||
return new LuaUpdateModel(luaName, method.Name, kind);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string FormatDouble(double value) =>
|
|
||||||
value.ToString("R", System.Globalization.CultureInfo.InvariantCulture) + "d";
|
|
||||||
|
|
||||||
private static string RegistrationHintName(LuaTableModel model)
|
|
||||||
{
|
|
||||||
var chain = string.Join(".", model.TypeChain.Select(static p => p.Name));
|
|
||||||
return $"{model.Namespace}.{chain}.LuaRegistration.g.cs";
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string EmitRegistration(LuaTableModel model)
|
|
||||||
{
|
|
||||||
var builder = new StringBuilder();
|
|
||||||
builder.AppendLine("#nullable enable");
|
|
||||||
builder.AppendLine($"namespace {model.Namespace}");
|
|
||||||
builder.AppendLine("{");
|
|
||||||
|
|
||||||
string indent = " ";
|
|
||||||
foreach (var part in model.TypeChain)
|
|
||||||
{
|
|
||||||
builder.AppendLine($"{indent}{(part.IsStatic ? "static partial class " : "partial class ")}{part.Name}");
|
|
||||||
builder.AppendLine($"{indent}{{");
|
|
||||||
indent += " ";
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isStatic = model.TypeChain[model.TypeChain.Count - 1].IsStatic;
|
|
||||||
string body = indent + " ";
|
|
||||||
|
|
||||||
if (model.Constants.Count > 0 || model.Functions.Count > 0)
|
|
||||||
{
|
|
||||||
builder.AppendLine($"{indent}internal {(isStatic ? "static " : "")}void RegisterLuaMembers(global::MoonSharp.Interpreter.Table table)");
|
|
||||||
builder.AppendLine($"{indent}{{");
|
|
||||||
|
|
||||||
foreach (var constant in model.Constants)
|
|
||||||
builder.AppendLine($"{body}table[\"{constant.LuaName}\"] = {constant.MemberName};");
|
|
||||||
|
|
||||||
foreach (var function in model.Functions)
|
|
||||||
{
|
|
||||||
string call = $"{function.MethodName}({EmitArguments(function.Parameters)})";
|
|
||||||
string lambda = function.ReturnKind switch
|
|
||||||
{
|
|
||||||
LuaReturnKind.DynValue => $"(execCtx, args) => {call}",
|
|
||||||
LuaReturnKind.Double => $"(execCtx, args) => global::MoonSharp.Interpreter.DynValue.NewNumber({call})",
|
|
||||||
_ => $"(execCtx, args) => {{ {call}; return global::MoonSharp.Interpreter.DynValue.Void; }}",
|
|
||||||
};
|
|
||||||
builder.AppendLine($"{body}table[\"{function.LuaName}\"] = global::MoonSharp.Interpreter.DynValue.NewCallback({lambda});");
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.AppendLine($"{indent}}}");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (model.Updates.Count > 0)
|
|
||||||
{
|
|
||||||
builder.AppendLine($"{indent}internal {(isStatic ? "static " : "")}void UpdateLuaMembers(global::MoonSharp.Interpreter.Table table, global::{model.ContextType} context)");
|
|
||||||
builder.AppendLine($"{indent}{{");
|
|
||||||
foreach (var update in model.Updates)
|
|
||||||
{
|
|
||||||
switch (update.Kind)
|
|
||||||
{
|
|
||||||
case LuaUpdateKind.OptionalValue:
|
|
||||||
builder.AppendLine($"{body}{{ var value = {update.MethodName}(context); if (value.HasValue) table[\"{update.LuaName}\"] = value.Value; }}");
|
|
||||||
break;
|
|
||||||
case LuaUpdateKind.OptionalReference:
|
|
||||||
builder.AppendLine($"{body}{{ var value = {update.MethodName}(context); if (value is not null) table[\"{update.LuaName}\"] = value; }}");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
builder.AppendLine($"{body}table[\"{update.LuaName}\"] = {update.MethodName}(context);");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
builder.AppendLine($"{indent}}}");
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = model.TypeChain.Count - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
indent = new string(' ', 4 * (i + 1));
|
|
||||||
builder.AppendLine($"{indent}}}");
|
|
||||||
}
|
|
||||||
builder.AppendLine("}");
|
|
||||||
return builder.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string EmitArguments(EquatableArray<LuaParameterModel> parameters)
|
|
||||||
{
|
|
||||||
var rendered = new List<string>();
|
|
||||||
int position = 0;
|
|
||||||
foreach (var parameter in parameters)
|
|
||||||
{
|
|
||||||
if (parameter.Kind == LuaParameterKind.ExecutionContext)
|
|
||||||
{
|
|
||||||
rendered.Add("execCtx");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (parameter.Kind == LuaParameterKind.Arguments)
|
|
||||||
{
|
|
||||||
rendered.Add("args");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
rendered.Add(parameter.Kind switch
|
|
||||||
{
|
|
||||||
LuaParameterKind.DynValue => $"args.Count > {position} ? args[{position}] : global::MoonSharp.Interpreter.DynValue.Nil",
|
|
||||||
LuaParameterKind.Double => $"args.Count > {position} ? args[{position}].CastToNumber() ?? {parameter.DefaultLiteral} : {parameter.DefaultLiteral}",
|
|
||||||
LuaParameterKind.Int => $"(int)(args.Count > {position} ? args[{position}].CastToNumber() ?? {parameter.DefaultLiteral} : {parameter.DefaultLiteral})",
|
|
||||||
_ => $"args.Count > {position} ? args[{position}].CastToBool() : {parameter.DefaultLiteral}",
|
|
||||||
});
|
|
||||||
position++;
|
|
||||||
}
|
|
||||||
return string.Join(", ", rendered);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string EmitCatalog(LuaCatalogEntry[] entries)
|
|
||||||
{
|
|
||||||
var builder = new StringBuilder();
|
|
||||||
builder.AppendLine("#nullable enable");
|
|
||||||
builder.AppendLine("namespace LuaScript.Generated");
|
|
||||||
builder.AppendLine("{");
|
|
||||||
builder.AppendLine(" internal static class LuaApiModuleInitializer");
|
|
||||||
builder.AppendLine(" {");
|
|
||||||
builder.AppendLine(" [global::System.Runtime.CompilerServices.ModuleInitializer]");
|
|
||||||
builder.AppendLine(" internal static void Initialize()");
|
|
||||||
builder.AppendLine(" {");
|
|
||||||
builder.AppendLine(" global::LuaScript.Api.LuaApiCatalog.Register(new global::LuaScript.Api.LuaApiMember[]");
|
|
||||||
builder.AppendLine(" {");
|
|
||||||
foreach (var entry in entries)
|
|
||||||
{
|
|
||||||
string kind = entry.IsFunction
|
|
||||||
? "global::LuaScript.Api.LuaApiMemberKind.Function"
|
|
||||||
: "global::LuaScript.Api.LuaApiMemberKind.Variable";
|
|
||||||
string parameters = entry.Parameters.Count == 0
|
|
||||||
? "global::System.Array.Empty<string>()"
|
|
||||||
: $"new string[] {{ {string.Join(", ", entry.Parameters.Select(static p => $"\"{p}\""))} }}";
|
|
||||||
builder.AppendLine($" new(\"{entry.Table}\", \"{entry.Name}\", {kind}, {parameters}),");
|
|
||||||
}
|
|
||||||
builder.AppendLine(" });");
|
|
||||||
builder.AppendLine(" }");
|
|
||||||
builder.AppendLine(" }");
|
|
||||||
builder.AppendLine("}");
|
|
||||||
return builder.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IReadOnlyList<LuaCatalogEntry> CollectAssemblyEntries(Compilation compilation)
|
|
||||||
{
|
|
||||||
var entries = new List<(int Position, LuaCatalogEntry Entry)>();
|
|
||||||
foreach (var attribute in compilation.Assembly.GetAttributes())
|
|
||||||
{
|
|
||||||
string? attributeName = attribute.AttributeClass?.ToDisplayString();
|
|
||||||
if (attributeName is not (VariableAttributeName or BuiltinAttributeName or FunctionAttributeName))
|
|
||||||
continue;
|
|
||||||
var entry = CreateAttributeEntry(attribute, string.Empty);
|
|
||||||
if (entry is null)
|
|
||||||
continue;
|
|
||||||
entries.Add((attribute.ApplicationSyntaxReference?.Span.Start ?? 0, entry));
|
|
||||||
}
|
|
||||||
return [.. entries.OrderBy(static e => e.Position).Select(static e => e.Entry)];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
|
||||||
<LangVersion>latest</LangVersion>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
<Version>1.16.0</Version>
|
|
||||||
<IsRoslynComponent>true</IsRoslynComponent>
|
|
||||||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="5.3.0">
|
|
||||||
<PrivateAssets>all</PrivateAssets>
|
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
|
||||||
</PackageReference>
|
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.3.0" PrivateAssets="all" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Immutable;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using Microsoft.CodeAnalysis;
|
|
||||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
||||||
using Microsoft.CodeAnalysis.Text;
|
|
||||||
|
|
||||||
namespace LuaScript.Generator
|
|
||||||
{
|
|
||||||
internal sealed record LuaSyntaxRuleModel(string TypeName, int Order);
|
|
||||||
|
|
||||||
[Generator]
|
|
||||||
public sealed class LuaSyntaxRuleGenerator : IIncrementalGenerator
|
|
||||||
{
|
|
||||||
private const string RuleAttributeName = "LuaScript.Compat.Syntax.LuaSyntaxRuleAttribute";
|
|
||||||
private const string RuleInterfaceName = "LuaScript.Compat.Syntax.ILuaSyntaxRule";
|
|
||||||
|
|
||||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
|
||||||
{
|
|
||||||
var rules = context.SyntaxProvider
|
|
||||||
.ForAttributeWithMetadataName(
|
|
||||||
RuleAttributeName,
|
|
||||||
static (node, _) => node is ClassDeclarationSyntax,
|
|
||||||
static (ctx, _) => CreateModel(ctx))
|
|
||||||
.Where(static model => model is not null)
|
|
||||||
.Select(static (model, _) => model!);
|
|
||||||
|
|
||||||
context.RegisterSourceOutput(rules.Collect(), static (ctx, models) =>
|
|
||||||
{
|
|
||||||
if (models.Length == 0)
|
|
||||||
return;
|
|
||||||
ctx.AddSource("LuaSyntaxRuleRegistry.g.cs", SourceText.From(Emit(models), Encoding.UTF8));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static LuaSyntaxRuleModel? CreateModel(GeneratorAttributeSyntaxContext context)
|
|
||||||
{
|
|
||||||
if (context.TargetSymbol is not INamedTypeSymbol symbol)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
if (!symbol.AllInterfaces.Any(static contract => contract.ToDisplayString() == RuleInterfaceName))
|
|
||||||
return null;
|
|
||||||
|
|
||||||
int order = 0;
|
|
||||||
var arguments = context.Attributes[0].ConstructorArguments;
|
|
||||||
if (arguments.Length > 0 && arguments[0].Value is int value)
|
|
||||||
order = value;
|
|
||||||
|
|
||||||
return new LuaSyntaxRuleModel(symbol.ToDisplayString(), order);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string Emit(ImmutableArray<LuaSyntaxRuleModel> models)
|
|
||||||
{
|
|
||||||
var ordered = models
|
|
||||||
.OrderBy(static m => m.Order)
|
|
||||||
.ThenBy(static m => m.TypeName, StringComparer.Ordinal)
|
|
||||||
.ToArray();
|
|
||||||
|
|
||||||
var builder = new StringBuilder();
|
|
||||||
builder.AppendLine("#nullable enable");
|
|
||||||
builder.AppendLine("namespace LuaScript.Compat.Syntax");
|
|
||||||
builder.AppendLine("{");
|
|
||||||
builder.AppendLine(" internal static class LuaSyntaxRuleRegistry");
|
|
||||||
builder.AppendLine(" {");
|
|
||||||
builder.AppendLine(" internal static readonly global::LuaScript.Compat.Syntax.ILuaSyntaxRule[] Rules = new global::LuaScript.Compat.Syntax.ILuaSyntaxRule[]");
|
|
||||||
builder.AppendLine(" {");
|
|
||||||
foreach (var model in ordered)
|
|
||||||
builder.AppendLine($" new global::{model.TypeName}(),");
|
|
||||||
builder.AppendLine(" };");
|
|
||||||
builder.AppendLine(" }");
|
|
||||||
builder.AppendLine("}");
|
|
||||||
return builder.ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
using System.Diagnostics;
|
|
||||||
using System.Runtime.Intrinsics;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed class CpuKernel(CpuKernel.RowDelegate row, CpuKernel.RowDelegate? block)
|
|
||||||
{
|
|
||||||
public delegate void RowDelegate(byte[] buffer, int rowBase, int xStart, int xEnd, double y, double[] uniforms);
|
|
||||||
|
|
||||||
private const int ParallelThreshold = 64;
|
|
||||||
private const int WarmupRunsPerPath = 1;
|
|
||||||
private const int TimedRunsPerPath = 3;
|
|
||||||
|
|
||||||
private readonly RowDelegate _row = row;
|
|
||||||
private readonly RowDelegate? _block = block;
|
|
||||||
private long _rowTicks;
|
|
||||||
private long _blockTicks;
|
|
||||||
private int _rowRuns;
|
|
||||||
private int _blockRuns;
|
|
||||||
private bool _nextIsBlock = true;
|
|
||||||
private bool _decided;
|
|
||||||
private bool _useBlock;
|
|
||||||
|
|
||||||
public bool? BlockAdopted => _decided ? _useBlock : null;
|
|
||||||
|
|
||||||
public void Execute(byte[] buffer, int width, int height, double[] uniforms)
|
|
||||||
{
|
|
||||||
if (_block is null || !Vector256.IsHardwareAccelerated)
|
|
||||||
{
|
|
||||||
Run(buffer, width, height, uniforms, false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_decided)
|
|
||||||
{
|
|
||||||
Run(buffer, width, height, uniforms, _useBlock);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool useBlock = _nextIsBlock;
|
|
||||||
_nextIsBlock = !_nextIsBlock;
|
|
||||||
|
|
||||||
long start = Stopwatch.GetTimestamp();
|
|
||||||
Run(buffer, width, height, uniforms, useBlock);
|
|
||||||
long elapsed = Stopwatch.GetTimestamp() - start;
|
|
||||||
|
|
||||||
if (useBlock)
|
|
||||||
{
|
|
||||||
if (_blockRuns >= WarmupRunsPerPath)
|
|
||||||
_blockTicks += elapsed;
|
|
||||||
_blockRuns++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (_rowRuns >= WarmupRunsPerPath)
|
|
||||||
_rowTicks += elapsed;
|
|
||||||
_rowRuns++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_rowRuns >= WarmupRunsPerPath + TimedRunsPerPath &&
|
|
||||||
_blockRuns >= WarmupRunsPerPath + TimedRunsPerPath)
|
|
||||||
{
|
|
||||||
_useBlock = _blockTicks < _rowTicks;
|
|
||||||
_decided = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Run(byte[] buffer, int width, int height, double[] uniforms, bool useBlock)
|
|
||||||
{
|
|
||||||
var row = _row;
|
|
||||||
var block = useBlock ? _block : null;
|
|
||||||
int stride = width * 4;
|
|
||||||
int blockWidth = block is null ? 0 : width & ~3;
|
|
||||||
|
|
||||||
void RunRow(int y)
|
|
||||||
{
|
|
||||||
int rowBase = y * stride;
|
|
||||||
if (blockWidth > 0)
|
|
||||||
block!(buffer, rowBase, 0, blockWidth, y, uniforms);
|
|
||||||
if (blockWidth < width)
|
|
||||||
row(buffer, rowBase, blockWidth, width, y, uniforms);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (height < ParallelThreshold)
|
|
||||||
{
|
|
||||||
for (int y = 0; y < height; y++)
|
|
||||||
RunRow(y);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Parallel.For(0, height, RunRow);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,270 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Runtime.Intrinsics;
|
|
||||||
|
|
||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal static class SimdKernelCompiler
|
|
||||||
{
|
|
||||||
private static readonly Type Vec = typeof(Vector256<double>);
|
|
||||||
|
|
||||||
private static readonly MethodInfo Broadcast = typeof(Vector256).GetMethod(nameof(Vector256.Create), [typeof(double)])!;
|
|
||||||
private static readonly MethodInfo Create4 = typeof(Vector256).GetMethod(nameof(Vector256.Create), [typeof(double), typeof(double), typeof(double), typeof(double)])!;
|
|
||||||
private static readonly MethodInfo GetElement = VecOp(nameof(Vector256.GetElement), 1, 1);
|
|
||||||
private static readonly MethodInfo Add = VecOp(nameof(Vector256.Add), 2);
|
|
||||||
private static readonly MethodInfo Subtract = VecOp(nameof(Vector256.Subtract), 2);
|
|
||||||
private static readonly MethodInfo Multiply = VecOp(nameof(Vector256.Multiply), 2);
|
|
||||||
private static readonly MethodInfo Divide = VecOp(nameof(Vector256.Divide), 2);
|
|
||||||
private static readonly MethodInfo Negate = VecOp(nameof(Vector256.Negate), 1);
|
|
||||||
private static readonly MethodInfo Abs = VecOp(nameof(Vector256.Abs), 1);
|
|
||||||
private static readonly MethodInfo Sqrt = VecOp(nameof(Vector256.Sqrt), 1);
|
|
||||||
private static readonly MethodInfo FloorFn = typeof(Vector256).GetMethod(nameof(Vector256.Floor), [Vec])!;
|
|
||||||
private static readonly MethodInfo CeilingFn = typeof(Vector256).GetMethod(nameof(Vector256.Ceiling), [Vec])!;
|
|
||||||
private static readonly MethodInfo ConditionalSelect = VecOp(nameof(Vector256.ConditionalSelect), 3);
|
|
||||||
private static readonly MethodInfo LessThan = VecOp(nameof(Vector256.LessThan), 2);
|
|
||||||
private static readonly MethodInfo GreaterThan = VecOp(nameof(Vector256.GreaterThan), 2);
|
|
||||||
private static readonly MethodInfo LessThanOrEqual = VecOp(nameof(Vector256.LessThanOrEqual), 2);
|
|
||||||
private static readonly MethodInfo GreaterThanOrEqual = VecOp(nameof(Vector256.GreaterThanOrEqual), 2);
|
|
||||||
private static readonly MethodInfo EqualsFn = VecOp(nameof(Vector256.Equals), 2);
|
|
||||||
private static readonly MethodInfo BitwiseAnd = VecOp(nameof(Vector256.BitwiseAnd), 2);
|
|
||||||
private static readonly MethodInfo BitwiseOr = VecOp(nameof(Vector256.BitwiseOr), 2);
|
|
||||||
private static readonly MethodInfo OnesComplement = VecOp(nameof(Vector256.OnesComplement), 1);
|
|
||||||
|
|
||||||
private static readonly Expression Zero = Expression.Constant(Vector256<double>.Zero, Vec);
|
|
||||||
private static readonly Expression AllBits = Expression.Constant(Vector256<double>.AllBitsSet, Vec);
|
|
||||||
private static readonly Expression V255 = Expression.Constant(Vector256.Create(255d), Vec);
|
|
||||||
|
|
||||||
public static CpuKernel.RowDelegate? TryCompile(KernelProgram program)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return Compile(program);
|
|
||||||
}
|
|
||||||
catch (KernelUnsupportedException)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static CpuKernel.RowDelegate Compile(KernelProgram program)
|
|
||||||
{
|
|
||||||
var buffer = Expression.Parameter(typeof(byte[]), "buffer");
|
|
||||||
var rowBase = Expression.Parameter(typeof(int), "rowBase");
|
|
||||||
var xStart = Expression.Parameter(typeof(int), "xStart");
|
|
||||||
var xEnd = Expression.Parameter(typeof(int), "xEnd");
|
|
||||||
var yScalar = Expression.Parameter(typeof(double), "y");
|
|
||||||
var uniforms = Expression.Parameter(typeof(double[]), "uniforms");
|
|
||||||
|
|
||||||
var x = Expression.Variable(typeof(int), "x");
|
|
||||||
var index = Expression.Variable(typeof(int), "index");
|
|
||||||
var yVec = Expression.Variable(Vec, "yVec");
|
|
||||||
var xVec = Expression.Variable(Vec, "xVec");
|
|
||||||
var alphaRaw = Expression.Variable(Vec, "alphaRaw");
|
|
||||||
var transparent = Expression.Variable(Vec, "transparent");
|
|
||||||
var scale = Expression.Variable(Vec, "scale");
|
|
||||||
var inputs = new[]
|
|
||||||
{
|
|
||||||
Expression.Variable(Vec, "inR"),
|
|
||||||
Expression.Variable(Vec, "inG"),
|
|
||||||
Expression.Variable(Vec, "inB"),
|
|
||||||
Expression.Variable(Vec, "inA"),
|
|
||||||
};
|
|
||||||
var slots = new ParameterExpression[program.Bindings.Count];
|
|
||||||
for (int i = 0; i < slots.Length; i++)
|
|
||||||
slots[i] = Expression.Variable(Vec, "slot" + i);
|
|
||||||
|
|
||||||
var context = new Context(xVec, yVec, uniforms, inputs, slots);
|
|
||||||
|
|
||||||
var statements = new List<Expression>
|
|
||||||
{
|
|
||||||
Expression.Assign(index, Expression.Add(rowBase, Expression.Multiply(x, Constant(4)))),
|
|
||||||
Expression.Assign(xVec, Expression.Call(Create4,
|
|
||||||
ToDouble(x),
|
|
||||||
ToDouble(Expression.Add(x, Constant(1))),
|
|
||||||
ToDouble(Expression.Add(x, Constant(2))),
|
|
||||||
ToDouble(Expression.Add(x, Constant(3))))),
|
|
||||||
Expression.Assign(alphaRaw, LoadChannel(buffer, index, 3)),
|
|
||||||
Expression.Assign(transparent, Call(LessThanOrEqual, alphaRaw, Zero)),
|
|
||||||
Expression.Assign(scale, Select(transparent, Zero, Call(Divide, V255, alphaRaw))),
|
|
||||||
Expression.Assign(inputs[3], Select(transparent, Zero, alphaRaw)),
|
|
||||||
Expression.Assign(inputs[0], Select(transparent, Zero, ClampByte(Call(Multiply, LoadChannel(buffer, index, 2), scale)))),
|
|
||||||
Expression.Assign(inputs[1], Select(transparent, Zero, ClampByte(Call(Multiply, LoadChannel(buffer, index, 1), scale)))),
|
|
||||||
Expression.Assign(inputs[2], Select(transparent, Zero, ClampByte(Call(Multiply, LoadChannel(buffer, index, 0), scale)))),
|
|
||||||
};
|
|
||||||
|
|
||||||
for (int i = 0; i < slots.Length; i++)
|
|
||||||
statements.Add(Expression.Assign(slots[i], Build(program.Bindings[i], context)));
|
|
||||||
|
|
||||||
var outA = Expression.Variable(Vec, "outA");
|
|
||||||
var alphaScale = Expression.Variable(Vec, "alphaScale");
|
|
||||||
statements.Add(Expression.Assign(outA, Build(program.OutputA, context)));
|
|
||||||
statements.Add(Expression.Assign(alphaScale, Call(Divide, ClampByte(outA), V255)));
|
|
||||||
statements.Add(StoreChannel(buffer, index, 0, ClampByte(Call(Multiply, Build(program.OutputB, context), alphaScale))));
|
|
||||||
statements.Add(StoreChannel(buffer, index, 1, ClampByte(Call(Multiply, Build(program.OutputG, context), alphaScale))));
|
|
||||||
statements.Add(StoreChannel(buffer, index, 2, ClampByte(Call(Multiply, Build(program.OutputR, context), alphaScale))));
|
|
||||||
statements.Add(StoreChannel(buffer, index, 3, ClampByte(outA)));
|
|
||||||
statements.Add(Expression.AddAssign(x, Constant(4)));
|
|
||||||
|
|
||||||
var breakLabel = Expression.Label("break");
|
|
||||||
var loop = Expression.Loop(
|
|
||||||
Expression.IfThenElse(
|
|
||||||
Expression.LessThan(x, xEnd),
|
|
||||||
Expression.Block(statements),
|
|
||||||
Expression.Break(breakLabel)),
|
|
||||||
breakLabel);
|
|
||||||
|
|
||||||
var locals = new List<ParameterExpression> { x, index, yVec, xVec, alphaRaw, transparent, scale, outA, alphaScale };
|
|
||||||
locals.AddRange(inputs);
|
|
||||||
locals.AddRange(slots);
|
|
||||||
|
|
||||||
var body = Expression.Block(
|
|
||||||
locals,
|
|
||||||
Expression.Assign(yVec, Expression.Call(Broadcast, yScalar)),
|
|
||||||
Expression.Assign(x, xStart),
|
|
||||||
loop);
|
|
||||||
|
|
||||||
var lambda = Expression.Lambda<CpuKernel.RowDelegate>(body, buffer, rowBase, xStart, xEnd, yScalar, uniforms);
|
|
||||||
return lambda.Compile();
|
|
||||||
}
|
|
||||||
|
|
||||||
private sealed record Context(
|
|
||||||
Expression X,
|
|
||||||
Expression Y,
|
|
||||||
ParameterExpression Uniforms,
|
|
||||||
Expression[] Inputs,
|
|
||||||
ParameterExpression[] Slots);
|
|
||||||
|
|
||||||
private static Expression Build(KExpr expression, Context context)
|
|
||||||
{
|
|
||||||
switch (expression)
|
|
||||||
{
|
|
||||||
case KConst value:
|
|
||||||
return Expression.Constant(Vector256.Create(value.Value), Vec);
|
|
||||||
case KInput input:
|
|
||||||
return context.Inputs[(int)input.Channel];
|
|
||||||
case KCoord coord:
|
|
||||||
return coord.Axis == KAxis.X ? context.X : context.Y;
|
|
||||||
case KUniformRef uniform:
|
|
||||||
return Expression.Call(Broadcast, Expression.ArrayIndex(context.Uniforms, Constant((int)uniform.Uniform)));
|
|
||||||
case KLocalRef local:
|
|
||||||
return context.Slots[local.Slot];
|
|
||||||
case KNegate negate:
|
|
||||||
return Call(Negate, Build(negate.Operand, context));
|
|
||||||
case KArith arithmetic:
|
|
||||||
return BuildArithmetic(arithmetic, context);
|
|
||||||
case KCall call:
|
|
||||||
return BuildCall(call, context);
|
|
||||||
case KSelect select:
|
|
||||||
return Select(BuildBool(select.Condition, context), Build(select.WhenTrue, context), Build(select.WhenFalse, context));
|
|
||||||
}
|
|
||||||
throw new KernelUnsupportedException("Unsupported SIMD expression.");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Expression BuildArithmetic(KArith arithmetic, Context context)
|
|
||||||
{
|
|
||||||
var left = Build(arithmetic.Left, context);
|
|
||||||
var right = Build(arithmetic.Right, context);
|
|
||||||
return arithmetic.Op switch
|
|
||||||
{
|
|
||||||
KArithOp.Add => Call(Add, left, right),
|
|
||||||
KArithOp.Subtract => Call(Subtract, left, right),
|
|
||||||
KArithOp.Multiply => Call(Multiply, left, right),
|
|
||||||
KArithOp.Divide => Call(Divide, left, right),
|
|
||||||
KArithOp.Modulo => Call(Subtract, left, Call(Multiply, Call(FloorFn, Call(Divide, left, right)), right)),
|
|
||||||
_ => throw new KernelUnsupportedException("Unsupported SIMD arithmetic operator."),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Expression BuildCall(KCall call, Context context)
|
|
||||||
{
|
|
||||||
var argument = Build(call.Arguments[0], context);
|
|
||||||
return call.Func switch
|
|
||||||
{
|
|
||||||
KFunc.Abs => Call(Abs, argument),
|
|
||||||
KFunc.Floor => Call(FloorFn, argument),
|
|
||||||
KFunc.Ceil => Call(CeilingFn, argument),
|
|
||||||
KFunc.Sqrt => Call(Sqrt, argument),
|
|
||||||
_ => throw new KernelUnsupportedException("Unsupported SIMD function."),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Expression BuildBool(KBool condition, Context context)
|
|
||||||
{
|
|
||||||
switch (condition)
|
|
||||||
{
|
|
||||||
case KBoolConst value:
|
|
||||||
return value.Value ? AllBits : Zero;
|
|
||||||
case KNot not:
|
|
||||||
return Call(OnesComplement, BuildBool(not.Operand, context));
|
|
||||||
case KLogical logical:
|
|
||||||
return logical.IsAnd
|
|
||||||
? Call(BitwiseAnd, BuildBool(logical.Left, context), BuildBool(logical.Right, context))
|
|
||||||
: Call(BitwiseOr, BuildBool(logical.Left, context), BuildBool(logical.Right, context));
|
|
||||||
case KCompare compare:
|
|
||||||
var left = Build(compare.Left, context);
|
|
||||||
var right = Build(compare.Right, context);
|
|
||||||
return compare.Op switch
|
|
||||||
{
|
|
||||||
KCompareOp.Less => Call(LessThan, left, right),
|
|
||||||
KCompareOp.Greater => Call(GreaterThan, left, right),
|
|
||||||
KCompareOp.LessEqual => Call(LessThanOrEqual, left, right),
|
|
||||||
KCompareOp.GreaterEqual => Call(GreaterThanOrEqual, left, right),
|
|
||||||
KCompareOp.Equal => Call(EqualsFn, left, right),
|
|
||||||
KCompareOp.NotEqual => Call(OnesComplement, Call(EqualsFn, left, right)),
|
|
||||||
_ => throw new KernelUnsupportedException("Unsupported SIMD comparison."),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
throw new KernelUnsupportedException("Unsupported SIMD condition.");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Expression ClampByte(Expression value)
|
|
||||||
{
|
|
||||||
var low = Select(Call(LessThan, value, Zero), Zero, value);
|
|
||||||
return Select(Call(GreaterThan, low, V255), V255, low);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Expression Select(Expression condition, Expression whenTrue, Expression whenFalse) =>
|
|
||||||
Expression.Call(ConditionalSelect, condition, whenTrue, whenFalse);
|
|
||||||
|
|
||||||
private static Expression LoadChannel(ParameterExpression buffer, ParameterExpression index, int offset) =>
|
|
||||||
Expression.Call(Create4,
|
|
||||||
ToDouble(ReadByte(buffer, index, offset)),
|
|
||||||
ToDouble(ReadByte(buffer, index, offset + 4)),
|
|
||||||
ToDouble(ReadByte(buffer, index, offset + 8)),
|
|
||||||
ToDouble(ReadByte(buffer, index, offset + 12)));
|
|
||||||
|
|
||||||
private static Expression StoreChannel(ParameterExpression buffer, ParameterExpression index, int offset, Expression value)
|
|
||||||
{
|
|
||||||
var writes = new Expression[4];
|
|
||||||
for (int lane = 0; lane < 4; lane++)
|
|
||||||
writes[lane] = Expression.Assign(
|
|
||||||
Expression.ArrayAccess(buffer, Expression.Add(index, Constant(lane * 4 + offset))),
|
|
||||||
Expression.Convert(Expression.Call(GetElement, value, Constant(lane)), typeof(byte)));
|
|
||||||
return Expression.Block(writes);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Expression ReadByte(ParameterExpression buffer, ParameterExpression index, int offset) =>
|
|
||||||
Expression.ArrayIndex(buffer, offset == 0 ? index : Expression.Add(index, Constant(offset)));
|
|
||||||
|
|
||||||
private static Expression Call(MethodInfo method, params Expression[] arguments) =>
|
|
||||||
Expression.Call(method, arguments);
|
|
||||||
|
|
||||||
private static Expression ToDouble(Expression value) => Expression.Convert(value, typeof(double));
|
|
||||||
|
|
||||||
private static ConstantExpression Constant(int value) => Expression.Constant(value, typeof(int));
|
|
||||||
|
|
||||||
private static MethodInfo VecOp(string name, int vectorArgs, int intArgs = 0) =>
|
|
||||||
typeof(Vector256).GetMethods()
|
|
||||||
.Single(m => m.Name == name && m.IsGenericMethodDefinition &&
|
|
||||||
m.GetParameters().Length == vectorArgs + intArgs &&
|
|
||||||
m.GetParameters().Take(vectorArgs).All(p => IsVector(p.ParameterType)) &&
|
|
||||||
m.GetParameters().Skip(vectorArgs).All(p => p.ParameterType == typeof(int)))
|
|
||||||
.MakeGenericMethod(typeof(double));
|
|
||||||
|
|
||||||
private static bool IsVector(Type type) =>
|
|
||||||
type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Vector256<>);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record KArith(KArithOp Op, KExpr Left, KExpr Right) : KExpr;
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal enum KArithOp
|
|
||||||
{
|
|
||||||
Add,
|
|
||||||
Subtract,
|
|
||||||
Multiply,
|
|
||||||
Divide,
|
|
||||||
Modulo,
|
|
||||||
Power,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal enum KAxis
|
|
||||||
{
|
|
||||||
X,
|
|
||||||
Y,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal abstract record KBool;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record KBoolConst(bool Value) : KBool;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record KCall(KFunc Func, IReadOnlyList<KExpr> Arguments) : KExpr;
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal enum KChannel
|
|
||||||
{
|
|
||||||
R,
|
|
||||||
G,
|
|
||||||
B,
|
|
||||||
A,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record KCompare(KCompareOp Op, KExpr Left, KExpr Right) : KBool;
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal enum KCompareOp
|
|
||||||
{
|
|
||||||
Less,
|
|
||||||
Greater,
|
|
||||||
LessEqual,
|
|
||||||
GreaterEqual,
|
|
||||||
Equal,
|
|
||||||
NotEqual,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record KConst(double Value) : KExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record KCoord(KAxis Axis) : KExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal abstract record KExpr;
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal enum KFunc
|
|
||||||
{
|
|
||||||
Abs,
|
|
||||||
Floor,
|
|
||||||
Ceil,
|
|
||||||
Sqrt,
|
|
||||||
Sin,
|
|
||||||
Cos,
|
|
||||||
Tan,
|
|
||||||
Asin,
|
|
||||||
Acos,
|
|
||||||
Atan,
|
|
||||||
Atan2,
|
|
||||||
Exp,
|
|
||||||
Log,
|
|
||||||
Pow,
|
|
||||||
Min,
|
|
||||||
Max,
|
|
||||||
Fmod,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal readonly record struct KFunctionInfo(KFunc Func, string Name, int MinArgs, int MaxArgs);
|
|
||||||
}
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal static class KFunctions
|
|
||||||
{
|
|
||||||
private static readonly KFunctionInfo[] Catalog =
|
|
||||||
[
|
|
||||||
new(KFunc.Abs, "abs", 1, 1),
|
|
||||||
new(KFunc.Floor, "floor", 1, 1),
|
|
||||||
new(KFunc.Ceil, "ceil", 1, 1),
|
|
||||||
new(KFunc.Sqrt, "sqrt", 1, 1),
|
|
||||||
new(KFunc.Sin, "sin", 1, 1),
|
|
||||||
new(KFunc.Cos, "cos", 1, 1),
|
|
||||||
new(KFunc.Tan, "tan", 1, 1),
|
|
||||||
new(KFunc.Asin, "asin", 1, 1),
|
|
||||||
new(KFunc.Acos, "acos", 1, 1),
|
|
||||||
new(KFunc.Atan, "atan", 1, 1),
|
|
||||||
new(KFunc.Atan2, "atan2", 2, 2),
|
|
||||||
new(KFunc.Exp, "exp", 1, 1),
|
|
||||||
new(KFunc.Log, "log", 1, 1),
|
|
||||||
new(KFunc.Pow, "pow", 2, 2),
|
|
||||||
new(KFunc.Min, "min", 2, int.MaxValue),
|
|
||||||
new(KFunc.Max, "max", 2, int.MaxValue),
|
|
||||||
new(KFunc.Fmod, "fmod", 2, 2),
|
|
||||||
];
|
|
||||||
|
|
||||||
private static readonly Dictionary<string, KFunctionInfo> ByName = BuildByName();
|
|
||||||
private static readonly string[] Intrinsics = BuildIntrinsics();
|
|
||||||
|
|
||||||
public static bool TryResolve(string name, out KFunctionInfo info) => ByName.TryGetValue(name, out info);
|
|
||||||
|
|
||||||
public static string Intrinsic(KFunc func) => Intrinsics[(int)func];
|
|
||||||
|
|
||||||
private static Dictionary<string, KFunctionInfo> BuildByName()
|
|
||||||
{
|
|
||||||
var map = new Dictionary<string, KFunctionInfo>(Catalog.Length, StringComparer.Ordinal);
|
|
||||||
foreach (var info in Catalog)
|
|
||||||
map[info.Name] = info;
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string[] BuildIntrinsics()
|
|
||||||
{
|
|
||||||
var names = new string[Enum.GetValues<KFunc>().Length];
|
|
||||||
foreach (var info in Catalog)
|
|
||||||
names[(int)info.Func] = info.Name;
|
|
||||||
return names;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record KInput(KChannel Channel) : KExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record KLocalRef(int Slot) : KExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record KLogical(bool IsAnd, KBool Left, KBool Right) : KBool;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record KNegate(KExpr Operand) : KExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record KNot(KBool Operand) : KBool;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record KSelect(KBool Condition, KExpr WhenTrue, KExpr WhenFalse) : KExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record KUniformRef(KernelUniform Uniform) : KExpr;
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record KernelProgram(
|
|
||||||
IReadOnlyList<KExpr> Bindings,
|
|
||||||
KExpr OutputR,
|
|
||||||
KExpr OutputG,
|
|
||||||
KExpr OutputB,
|
|
||||||
KExpr OutputA,
|
|
||||||
IReadOnlyList<KernelUniform> Uniforms);
|
|
||||||
}
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal enum KernelUniform
|
|
||||||
{
|
|
||||||
Width,
|
|
||||||
Height,
|
|
||||||
HalfWidth,
|
|
||||||
HalfHeight,
|
|
||||||
CenterX,
|
|
||||||
CenterY,
|
|
||||||
CenterZ,
|
|
||||||
Diagonal,
|
|
||||||
X,
|
|
||||||
Y,
|
|
||||||
Z,
|
|
||||||
Ox,
|
|
||||||
Oy,
|
|
||||||
Oz,
|
|
||||||
Sx,
|
|
||||||
Sy,
|
|
||||||
Sz,
|
|
||||||
Zoom,
|
|
||||||
Aspect,
|
|
||||||
Alpha,
|
|
||||||
Rx,
|
|
||||||
Ry,
|
|
||||||
Rz,
|
|
||||||
Rxr,
|
|
||||||
Ryr,
|
|
||||||
Rzr,
|
|
||||||
Track0,
|
|
||||||
Track1,
|
|
||||||
Track2,
|
|
||||||
Track3,
|
|
||||||
Slider0,
|
|
||||||
Slider1,
|
|
||||||
Slider2,
|
|
||||||
Slider3,
|
|
||||||
Check0,
|
|
||||||
Check1,
|
|
||||||
Check2,
|
|
||||||
Check3,
|
|
||||||
Color,
|
|
||||||
Time,
|
|
||||||
Frame,
|
|
||||||
TotalFrame,
|
|
||||||
TotalTime,
|
|
||||||
T,
|
|
||||||
Framerate,
|
|
||||||
Layer,
|
|
||||||
Index,
|
|
||||||
Num,
|
|
||||||
SceneWidth,
|
|
||||||
SceneHeight,
|
|
||||||
SceneCenterX,
|
|
||||||
SceneCenterY,
|
|
||||||
TimelineFrame,
|
|
||||||
TimelineTime,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record AssignStmt(IReadOnlyList<LuaExpr> Targets, IReadOnlyList<LuaExpr> Values) : LuaStmt;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record BinaryExpr(string Operator, LuaExpr Left, LuaExpr Right) : LuaExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record BoolExpr(bool Value) : LuaExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record CallExpr(LuaExpr Target, IReadOnlyList<LuaExpr> Arguments) : LuaExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record CallStmt(LuaExpr Call) : LuaStmt;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record IfClause(LuaExpr Condition, IReadOnlyList<LuaStmt> Body);
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record IfStmt(IReadOnlyList<IfClause> Clauses, IReadOnlyList<LuaStmt>? ElseBody) : LuaStmt;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record IndexExpr(LuaExpr Target, LuaExpr Key) : LuaExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record LocalStmt(IReadOnlyList<string> Names, IReadOnlyList<LuaExpr> Values) : LuaStmt;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal abstract record LuaExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal abstract record LuaStmt;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record MemberExpr(LuaExpr Target, string Name) : LuaExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record MethodCallExpr(LuaExpr Target, string Method, IReadOnlyList<LuaExpr> Arguments) : LuaExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record NameExpr(string Name) : LuaExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record NilExpr : LuaExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record NumberExpr(double Value) : LuaExpr;
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record NumericForStmt(
|
|
||||||
string Variable,
|
|
||||||
LuaExpr Start,
|
|
||||||
LuaExpr Stop,
|
|
||||||
LuaExpr? Step,
|
|
||||||
IReadOnlyList<LuaStmt> Body) : LuaStmt;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record StringExpr(string Value) : LuaExpr;
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal sealed record UnaryExpr(string Operator, LuaExpr Operand) : LuaExpr;
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
namespace LuaScript.Engine.Kernel
|
|
||||||
{
|
|
||||||
internal enum LuaTokenKind
|
|
||||||
{
|
|
||||||
Name,
|
|
||||||
Number,
|
|
||||||
String,
|
|
||||||
Keyword,
|
|
||||||
Symbol,
|
|
||||||
Eof,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>net10.0-windows10.0.19041.0</TargetFramework>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
<Version>1.16.0</Version>
|
|
||||||
<Platforms>AnyCPU;x64</Platforms>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<InternalsVisibleTo Include="LuaScript" />
|
|
||||||
<InternalsVisibleTo Include="LuaScript.Compat" />
|
|
||||||
<InternalsVisibleTo Include="LuaScript.Runtime" />
|
|
||||||
<InternalsVisibleTo Include="LuaScript.Tests" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>net10.0-windows10.0.19041.0</TargetFramework>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
<Version>1.16.0</Version>
|
|
||||||
<Platforms>AnyCPU;x64</Platforms>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<InternalsVisibleTo Include="LuaScript" />
|
|
||||||
<InternalsVisibleTo Include="LuaScript.Tests" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Reference Include="SharpGen.Runtime">
|
|
||||||
<HintPath>$(YMM4DirPath)SharpGen.Runtime.dll</HintPath>
|
|
||||||
<Private>false</Private>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="SharpGen.Runtime.COM">
|
|
||||||
<HintPath>$(YMM4DirPath)SharpGen.Runtime.COM.dll</HintPath>
|
|
||||||
<Private>false</Private>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="Vortice.Direct2D1">
|
|
||||||
<HintPath>$(YMM4DirPath)Vortice.Direct2D1.dll</HintPath>
|
|
||||||
<Private>false</Private>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="Vortice.DirectX">
|
|
||||||
<HintPath>$(YMM4DirPath)Vortice.DirectX.dll</HintPath>
|
|
||||||
<Private>false</Private>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="Vortice.DXGI.dll">
|
|
||||||
<HintPath>$(YMM4DirPath)Vortice.DXGI.dll</HintPath>
|
|
||||||
<Private>false</Private>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="Vortice.Mathematics">
|
|
||||||
<HintPath>$(YMM4DirPath)Vortice.Mathematics.dll</HintPath>
|
|
||||||
<Private>false</Private>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="Vortice.MediaFoundation">
|
|
||||||
<HintPath>$(YMM4DirPath)Vortice.MediaFoundation.dll</HintPath>
|
|
||||||
<Private>false</Private>
|
|
||||||
</Reference>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
namespace LuaScript.Anchor
|
|
||||||
{
|
|
||||||
internal enum AnchorConnection
|
|
||||||
{
|
|
||||||
None,
|
|
||||||
Line,
|
|
||||||
Loop,
|
|
||||||
Star,
|
|
||||||
Arm,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Anchor
|
|
||||||
{
|
|
||||||
internal readonly record struct AnchorOptions(AnchorConnection Connection, bool Is3D);
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace LuaScript.Anchor
|
|
||||||
{
|
|
||||||
internal readonly record struct AnchorRequestData(string Group, int Count, AnchorConnection Connection, bool Is3D);
|
|
||||||
}
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
using System.Collections.Immutable;
|
|
||||||
|
|
||||||
namespace LuaScript.Anchor
|
|
||||||
{
|
|
||||||
internal static class AnchorSupport
|
|
||||||
{
|
|
||||||
public const int MaxAnchors = 32;
|
|
||||||
public const string TrackGroup = "track";
|
|
||||||
|
|
||||||
public static int ClampCount(int count) => Math.Clamp(count, 0, MaxAnchors);
|
|
||||||
|
|
||||||
public static void ApplyOption(string option, ref AnchorConnection connection, ref bool is3D)
|
|
||||||
{
|
|
||||||
switch (option)
|
|
||||||
{
|
|
||||||
case "line": connection = AnchorConnection.Line; break;
|
|
||||||
case "loop": connection = AnchorConnection.Loop; break;
|
|
||||||
case "star": connection = AnchorConnection.Star; break;
|
|
||||||
case "arm": connection = AnchorConnection.Arm; break;
|
|
||||||
case "xyz": is3D = true; break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static (double X, double Y) DefaultPosition(int index)
|
|
||||||
{
|
|
||||||
int col = index % 8;
|
|
||||||
int row = index / 8;
|
|
||||||
return ((col - 3.5) * 60d, (row - 1.5) * 60d);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void ResolvePosition(IReadOnlyList<LuaAnchorPoint>? source, string group, int index,
|
|
||||||
out double x, out double y, out double z)
|
|
||||||
{
|
|
||||||
if (source is not null)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < source.Count; i++)
|
|
||||||
{
|
|
||||||
var a = source[i];
|
|
||||||
if (a.Index == index && string.Equals(a.Group, group, StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
x = a.X;
|
|
||||||
y = a.Y;
|
|
||||||
z = a.Z;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var (dx, dy) = DefaultPosition(index);
|
|
||||||
x = dx;
|
|
||||||
y = dy;
|
|
||||||
z = 0d;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ImmutableList<LuaAnchorPoint> ApplyDrag(
|
|
||||||
ImmutableList<LuaAnchorPoint> source, string group, int index, double dx, double dy, double dz)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < source.Count; i++)
|
|
||||||
{
|
|
||||||
var a = source[i];
|
|
||||||
if (a.Index == index && string.Equals(a.Group, group, StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
return source.SetItem(i, new LuaAnchorPoint
|
|
||||||
{
|
|
||||||
Group = group,
|
|
||||||
Index = index,
|
|
||||||
X = a.X + dx,
|
|
||||||
Y = a.Y + dy,
|
|
||||||
Z = a.Z + dz,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var (bx, by) = DefaultPosition(index);
|
|
||||||
return source.Add(new LuaAnchorPoint
|
|
||||||
{
|
|
||||||
Group = group,
|
|
||||||
Index = index,
|
|
||||||
X = bx + dx,
|
|
||||||
Y = by + dy,
|
|
||||||
Z = dz,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
namespace LuaScript.Anchor
|
|
||||||
{
|
|
||||||
public sealed class LuaAnchorPoint
|
|
||||||
{
|
|
||||||
public string Group { get; set; } = string.Empty;
|
|
||||||
public int Index { get; set; }
|
|
||||||
public double X { get; set; }
|
|
||||||
public double Y { get; set; }
|
|
||||||
public double Z { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
namespace LuaScript.Api
|
|
||||||
{
|
|
||||||
internal static class LuaApiCatalog
|
|
||||||
{
|
|
||||||
private static readonly object s_gate = new();
|
|
||||||
private static readonly List<LuaApiMember> s_members = [];
|
|
||||||
private static LuaApiMember[]? s_snapshot;
|
|
||||||
private static int s_version;
|
|
||||||
|
|
||||||
internal static int Version
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
lock (s_gate)
|
|
||||||
return s_version;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static void Register(IReadOnlyList<LuaApiMember> members)
|
|
||||||
{
|
|
||||||
lock (s_gate)
|
|
||||||
{
|
|
||||||
s_members.AddRange(members);
|
|
||||||
s_snapshot = null;
|
|
||||||
s_version++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static IReadOnlyList<LuaApiMember> Members
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
lock (s_gate)
|
|
||||||
return s_snapshot ??= [.. s_members];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
namespace LuaScript.Api
|
|
||||||
{
|
|
||||||
internal sealed record LuaApiMember(
|
|
||||||
string Table,
|
|
||||||
string Name,
|
|
||||||
LuaApiMemberKind Kind,
|
|
||||||
IReadOnlyList<string> Parameters)
|
|
||||||
{
|
|
||||||
public string QualifiedName => Table.Length == 0 ? Name : $"{Table}.{Name}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
namespace LuaScript.Api
|
|
||||||
{
|
|
||||||
internal enum LuaApiMemberKind
|
|
||||||
{
|
|
||||||
Function,
|
|
||||||
Variable,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
namespace LuaScript.Api
|
|
||||||
{
|
|
||||||
[LuaTable("")]
|
|
||||||
[LuaBuiltin("type", "v")]
|
|
||||||
[LuaBuiltin("tostring", "v")]
|
|
||||||
[LuaBuiltin("tonumber", "e", "base")]
|
|
||||||
[LuaBuiltin("select", "n", "...")]
|
|
||||||
[LuaBuiltin("error", "message", "level")]
|
|
||||||
[LuaBuiltin("assert", "v", "message")]
|
|
||||||
[LuaBuiltin("print", "...")]
|
|
||||||
[LuaBuiltin("ipairs", "t")]
|
|
||||||
[LuaBuiltin("pairs", "t")]
|
|
||||||
[LuaBuiltin("next", "table", "index")]
|
|
||||||
[LuaBuiltin("unpack", "list", "i", "j")]
|
|
||||||
[LuaBuiltin("setmetatable", "table", "metatable")]
|
|
||||||
[LuaBuiltin("getmetatable", "object")]
|
|
||||||
[LuaBuiltin("rawget", "table", "index")]
|
|
||||||
[LuaBuiltin("rawset", "table", "index", "value")]
|
|
||||||
[LuaBuiltin("rawequal", "v1", "v2")]
|
|
||||||
[LuaBuiltin("rawlen", "v")]
|
|
||||||
[LuaBuiltin("pcall", "f", "...")]
|
|
||||||
[LuaBuiltin("xpcall", "f", "msgh", "...")]
|
|
||||||
internal static class LuaBasicLibrary;
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
namespace LuaScript.Api
|
|
||||||
{
|
|
||||||
[LuaTable("bit32")]
|
|
||||||
[LuaBuiltin("band", "...")]
|
|
||||||
[LuaBuiltin("bor", "...")]
|
|
||||||
[LuaBuiltin("bxor", "...")]
|
|
||||||
[LuaBuiltin("bnot", "x")]
|
|
||||||
[LuaBuiltin("lshift", "x", "disp")]
|
|
||||||
[LuaBuiltin("rshift", "x", "disp")]
|
|
||||||
[LuaBuiltin("arshift", "x", "disp")]
|
|
||||||
[LuaBuiltin("extract", "n", "field", "width")]
|
|
||||||
[LuaBuiltin("replace", "n", "v", "field", "width")]
|
|
||||||
[LuaBuiltin("btest", "...")]
|
|
||||||
[LuaBuiltin("countlz", "x")]
|
|
||||||
[LuaBuiltin("countrz", "x")]
|
|
||||||
internal static class LuaBit32Library;
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
namespace LuaScript.Api
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)]
|
|
||||||
internal sealed class LuaBuiltinAttribute(string name, params string[] parameters) : Attribute
|
|
||||||
{
|
|
||||||
public string Name { get; } = name;
|
|
||||||
public string[] Parameters { get; } = parameters;
|
|
||||||
public string Table { get; init; } = string.Empty;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
namespace LuaScript.Api
|
|
||||||
{
|
|
||||||
internal static class LuaCompletionSource
|
|
||||||
{
|
|
||||||
internal static readonly string[] Keywords =
|
|
||||||
[
|
|
||||||
"and", "break", "do", "else", "elseif", "end", "false", "for",
|
|
||||||
"function", "goto", "if", "in", "local", "nil", "not", "or",
|
|
||||||
"repeat", "return", "then", "true", "until", "while"
|
|
||||||
];
|
|
||||||
|
|
||||||
private sealed record Snapshot(
|
|
||||||
int Version,
|
|
||||||
string[] StaticCandidates,
|
|
||||||
IReadOnlyDictionary<string, string[]> TableMembers);
|
|
||||||
|
|
||||||
private static Snapshot? s_snapshot;
|
|
||||||
|
|
||||||
internal static string[] StaticCandidates => Current().StaticCandidates;
|
|
||||||
|
|
||||||
internal static bool TryGetTableMembers(string table, out string[] members)
|
|
||||||
{
|
|
||||||
if (Current().TableMembers.TryGetValue(table, out var found))
|
|
||||||
{
|
|
||||||
members = found;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
members = [];
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Snapshot Current()
|
|
||||||
{
|
|
||||||
var snapshot = s_snapshot;
|
|
||||||
int version = LuaApiCatalog.Version;
|
|
||||||
if (snapshot is null || snapshot.Version != version)
|
|
||||||
s_snapshot = snapshot = Build(version);
|
|
||||||
return snapshot;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Snapshot Build(int version)
|
|
||||||
{
|
|
||||||
var members = LuaApiCatalog.Members;
|
|
||||||
|
|
||||||
var tableMembers = new Dictionary<string, List<string>>(StringComparer.Ordinal);
|
|
||||||
var globals = new List<string>();
|
|
||||||
foreach (var member in members)
|
|
||||||
{
|
|
||||||
if (member.Table.Length == 0)
|
|
||||||
{
|
|
||||||
globals.Add(member.Name);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!tableMembers.TryGetValue(member.Table, out var list))
|
|
||||||
{
|
|
||||||
list = [];
|
|
||||||
tableMembers[member.Table] = list;
|
|
||||||
}
|
|
||||||
list.Add(member.QualifiedName);
|
|
||||||
}
|
|
||||||
|
|
||||||
var candidates = Keywords
|
|
||||||
.Concat(globals)
|
|
||||||
.Concat(tableMembers.Keys)
|
|
||||||
.Concat(tableMembers.Values.SelectMany(x => x))
|
|
||||||
.Distinct()
|
|
||||||
.OrderBy(x => x.Length)
|
|
||||||
.ThenBy(x => x, StringComparer.OrdinalIgnoreCase)
|
|
||||||
.ToArray();
|
|
||||||
|
|
||||||
var frozen = new Dictionary<string, string[]>(StringComparer.Ordinal);
|
|
||||||
foreach (var (table, list) in tableMembers)
|
|
||||||
frozen[table] = [.. list.Distinct()];
|
|
||||||
|
|
||||||
return new Snapshot(version, candidates, frozen);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
namespace LuaScript.Api
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false)]
|
|
||||||
internal sealed class LuaConstantAttribute(string name) : Attribute
|
|
||||||
{
|
|
||||||
public string Name { get; } = name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
namespace LuaScript.Api
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)]
|
|
||||||
internal sealed class LuaFunctionAttribute(string name, params string[] parameters) : Attribute
|
|
||||||
{
|
|
||||||
public string Name { get; } = name;
|
|
||||||
public string[] Parameters { get; } = parameters;
|
|
||||||
public string Table { get; init; } = string.Empty;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
namespace LuaScript.Api
|
|
||||||
{
|
|
||||||
[LuaTable("math")]
|
|
||||||
[LuaBuiltin("abs", "x")]
|
|
||||||
[LuaBuiltin("ceil", "x")]
|
|
||||||
[LuaBuiltin("cos", "x")]
|
|
||||||
[LuaBuiltin("exp", "x")]
|
|
||||||
[LuaBuiltin("floor", "x")]
|
|
||||||
[LuaBuiltin("fmod", "x", "y")]
|
|
||||||
[LuaVariable("huge")]
|
|
||||||
[LuaBuiltin("log", "x", "base")]
|
|
||||||
[LuaBuiltin("max", "x", "...")]
|
|
||||||
[LuaBuiltin("min", "x", "...")]
|
|
||||||
[LuaBuiltin("modf", "x")]
|
|
||||||
[LuaVariable("pi")]
|
|
||||||
[LuaBuiltin("random", "m", "n")]
|
|
||||||
[LuaBuiltin("randomseed", "x")]
|
|
||||||
[LuaBuiltin("sin", "x")]
|
|
||||||
[LuaBuiltin("sqrt", "x")]
|
|
||||||
[LuaBuiltin("tan", "x")]
|
|
||||||
[LuaBuiltin("atan", "y", "x")]
|
|
||||||
[LuaBuiltin("atan2", "y", "x")]
|
|
||||||
[LuaBuiltin("deg", "x")]
|
|
||||||
[LuaBuiltin("rad", "x")]
|
|
||||||
[LuaBuiltin("ldexp", "m", "e")]
|
|
||||||
[LuaBuiltin("frexp", "x")]
|
|
||||||
[LuaBuiltin("sinh", "x")]
|
|
||||||
[LuaBuiltin("cosh", "x")]
|
|
||||||
[LuaBuiltin("tanh", "x")]
|
|
||||||
[LuaBuiltin("type", "x")]
|
|
||||||
[LuaBuiltin("tointeger", "x")]
|
|
||||||
[LuaVariable("maxinteger")]
|
|
||||||
[LuaVariable("mininteger")]
|
|
||||||
internal static class LuaMathLibrary;
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
namespace LuaScript.Api
|
|
||||||
{
|
|
||||||
[LuaTable("string")]
|
|
||||||
[LuaBuiltin("format", "formatstring", "...")]
|
|
||||||
[LuaBuiltin("len", "s")]
|
|
||||||
[LuaBuiltin("sub", "s", "i", "j")]
|
|
||||||
[LuaBuiltin("upper", "s")]
|
|
||||||
[LuaBuiltin("lower", "s")]
|
|
||||||
[LuaBuiltin("rep", "s", "n", "sep")]
|
|
||||||
[LuaBuiltin("reverse", "s")]
|
|
||||||
[LuaBuiltin("find", "s", "pattern", "init", "plain")]
|
|
||||||
[LuaBuiltin("match", "s", "pattern", "init")]
|
|
||||||
[LuaBuiltin("gmatch", "s", "pattern")]
|
|
||||||
[LuaBuiltin("gsub", "s", "pattern", "repl", "n")]
|
|
||||||
[LuaBuiltin("byte", "s", "i", "j")]
|
|
||||||
[LuaBuiltin("char", "...")]
|
|
||||||
[LuaBuiltin("dump", "function")]
|
|
||||||
[LuaBuiltin("pack", "fmt", "...")]
|
|
||||||
[LuaBuiltin("unpack", "fmt", "s", "pos")]
|
|
||||||
[LuaBuiltin("packsize", "fmt")]
|
|
||||||
internal static class LuaStringLibrary;
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
namespace LuaScript.Api
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
|
||||||
internal sealed class LuaTableAttribute(string name) : Attribute
|
|
||||||
{
|
|
||||||
public string Name { get; } = name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
264ファイル中100ファイルを読み込みました。 変更されたファイルが多すぎるため、それ以降のファイルは表示していません。
さらに表示
新しいイシューから参照
ユーザーをブロックする