├── start
├── test.groovy
├── compiler.groovy
└── wenyan.sh
├── .idea
├── .name
├── wenyan-lang_jvm.iml
├── dictionaries
│ └── luchangcun.xml
├── serialmonitor_settings.xml
├── vcs.xml
├── scala_compiler.xml
├── compiler.xml
├── misc.xml
├── wenyan-jvm.iml
└── uiDesigner.xml
├── project_example
├── src
│ └── main
│ │ └── java
│ │ ├── main
│ │ ├── hello
│ │ │ └── 其他文件.wy
│ │ └── 主文件.wy
│ │ └── 藏書樓
│ │ └── 子曰
│ │ ├── 君子
│ │ └── 序.wy
│ │ └── 序.wy
├── MakeFile.txt
├── project_example.iml
└── pom.xml
├── images
├── logo.png
├── color.png
├── turing.png
└── program.png
├── 关于文言文编程语言Java编译器.pdf
├── wenyan_lang_jvm.iml
├── src
├── main
│ └── java
│ │ └── cn
│ │ └── wenyan
│ │ ├── compiler
│ │ ├── Init.java
│ │ ├── RunCode.java
│ │ ├── utils
│ │ │ ├── PrettyCode.groovy
│ │ │ ├── ScalaUtils.scala
│ │ │ ├── ResultEntry.java
│ │ │ ├── SyntaxColor.java
│ │ │ ├── FileFormat.java
│ │ │ ├── SpecialSyntaxLexer.scala
│ │ │ ├── Assert.java
│ │ │ ├── GroovyPrettyCode.groovy
│ │ │ ├── VarLabel.java
│ │ │ ├── NumberTree.groovy
│ │ │ ├── GroovyUtils.groovy
│ │ │ ├── Utils.java
│ │ │ └── LexerUtils.scala
│ │ ├── log
│ │ │ ├── PluginLogger.java
│ │ │ ├── Message.java
│ │ │ ├── ILogger.java
│ │ │ ├── LogLevel.java
│ │ │ ├── LogFormat.java
│ │ │ ├── ServerLogger.java
│ │ │ └── LoggerUtil.java
│ │ ├── exceptions
│ │ │ ├── SyntaxException.java
│ │ │ └── CommandException.java
│ │ ├── Compile.java
│ │ ├── lib
│ │ │ ├── Defines.java
│ │ │ ├── Define.java
│ │ │ ├── WenYanException.java
│ │ │ ├── MathUtil.groovy
│ │ │ ├── ArrayUtils.groovy
│ │ │ └── JSArray.groovy
│ │ ├── LanguageCompiler.java
│ │ ├── plugins
│ │ │ ├── Main.java
│ │ │ ├── Listener.java
│ │ │ ├── JavaPluginClassloader.java
│ │ │ ├── Plugin.scala
│ │ │ ├── PluginManager.java
│ │ │ └── PHPPlugin.groovy
│ │ ├── WenYanCompiler.java
│ │ ├── command
│ │ │ ├── GroovyCommand.java
│ │ │ ├── StrongTypeCommand.java
│ │ │ ├── LexerViewerCommand.java
│ │ │ ├── OutFileCommand.java
│ │ │ ├── PinYinCommand.java
│ │ │ ├── SourceCommand.java
│ │ │ ├── WenYuanGeDownloadCommand.java
│ │ │ ├── ClassFileCommand.java
│ │ │ ├── MainClassCommand.java
│ │ │ ├── CompileLibCommand.java
│ │ │ ├── RunCommand.java
│ │ │ ├── WenYuanGeCommand.java
│ │ │ ├── Command.java
│ │ │ ├── CompileFileCommand.java
│ │ │ ├── CommandHandler.java
│ │ │ └── CompilerConfig.java
│ │ ├── WenYanTools.groovy
│ │ ├── factory
│ │ │ ├── StreamBuilder.java
│ │ │ └── CompileFactory.java
│ │ ├── test
│ │ │ ├── TestMain.java
│ │ │ └── PackStdLib.java
│ │ ├── CompileBackend.java
│ │ ├── streams
│ │ │ ├── CompileStream.java
│ │ │ ├── CommentCompileStream.java
│ │ │ ├── ObjectCompileStream.java
│ │ │ ├── ArrayCompileStream.java
│ │ │ ├── MathCompileStream.java
│ │ │ ├── TryCompileStream.java
│ │ │ └── ControlCompileStream.java
│ │ ├── WenYanRuntime.java
│ │ ├── CompileResult.java
│ │ ├── GroovyCompiler.java
│ │ ├── script
│ │ │ └── libs
│ │ │ │ ├── Library.groovy
│ │ │ │ ├── Syntax.java
│ │ │ │ ├── Language.groovy
│ │ │ │ └── LanguageUtils.groovy
│ │ ├── Main.java
│ │ ├── WenYanShell.java
│ │ ├── WenyanToGroovyBackend.java
│ │ └── PrepareCompiler.java
│ │ └── lang
│ │ ├── 西曆法.wy
│ │ ├── 畫譜.wy
│ │ ├── 籌經.wy
│ │ ├── 物經.wy
│ │ ├── 易經.wy
│ │ ├── 位經.wy
│ │ ├── 格物.wy
│ │ ├── 列經.wy
│ │ ├── 曆表.wy
│ │ └── 渾沌經.wy
└── test
│ └── lang
│ └── wenyan
│ └── test
│ └── TestImport.java
├── web
└── hello.html
├── .github
└── ISSUE_TEMPLATE
│ ├── custom.md
│ ├── feature_request.md
│ └── bug_report.md
├── CONTRIBUTING.md
├── LICENSE
├── book
└── var.md
├── document
└── Java-Compiler.md
├── CODE_OF_CONDUCT.md
└── pom.xml
/start/test.groovy:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | wenyan_lang
--------------------------------------------------------------------------------
/project_example/src/main/java/main/hello/其他文件.wy:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/project_example/src/main/java/藏書樓/子曰/君子/序.wy:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/project_example/src/main/java/main/主文件.wy:
--------------------------------------------------------------------------------
1 | 吾嘗觀「「子曰」」之書。方悟「子曰」之義。
2 |
3 | 子曰『君曰甚好』
--------------------------------------------------------------------------------
/images/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wenyan-lang/wenyan-jvm/HEAD/images/logo.png
--------------------------------------------------------------------------------
/images/color.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wenyan-lang/wenyan-jvm/HEAD/images/color.png
--------------------------------------------------------------------------------
/images/turing.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wenyan-lang/wenyan-jvm/HEAD/images/turing.png
--------------------------------------------------------------------------------
/images/program.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wenyan-lang/wenyan-jvm/HEAD/images/program.png
--------------------------------------------------------------------------------
/关于文言文编程语言Java编译器.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wenyan-lang/wenyan-jvm/HEAD/关于文言文编程语言Java编译器.pdf
--------------------------------------------------------------------------------
/wenyan_lang_jvm.iml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/.idea/wenyan-lang_jvm.iml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/project_example/MakeFile.txt:
--------------------------------------------------------------------------------
1 | /Users/luchangcun/Projects/new/wenyan-jvm/project_example/src/main/java/main/主文件.wy
--------------------------------------------------------------------------------
/project_example/project_example.iml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/start/compiler.groovy:
--------------------------------------------------------------------------------
1 | import cn.wenyan.compiler.WenYanTools
2 |
3 |
4 | print WenYanTools.default.dispatch("乃得一")
5 |
--------------------------------------------------------------------------------
/.idea/dictionaries/luchangcun.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/Init.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler;
2 |
3 | public interface Init {
4 |
5 | void init(WenYanCompilerImpl impl);
6 | }
7 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/RunCode.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler;
2 |
3 | public interface RunCode {
4 |
5 | Object run(boolean out,String... wenyanString);
6 | }
7 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/utils/PrettyCode.groovy:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.utils
2 |
3 | trait PrettyCode {
4 |
5 | abstract String pretty(String string)
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/.idea/serialmonitor_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/web/hello.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Title
6 |
7 |
8 | HelloWorld
9 |
10 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/custom.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Custom issue template
3 | about: Describe this issue template's purpose here.
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/scala_compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/log/PluginLogger.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.log;
2 |
3 | import java.io.File;
4 |
5 | public class PluginLogger extends ServerLogger {
6 |
7 | public PluginLogger(File parentDir, String prefix) {
8 | super(parentDir, prefix);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/exceptions/SyntaxException.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.exceptions;
2 |
3 | public class SyntaxException extends RuntimeException {
4 |
5 | public SyntaxException() {
6 | }
7 |
8 | public SyntaxException(String message) {
9 | super(message);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/Compile.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler;
2 |
3 | /**
4 | * 此为编译器之行动者乎,以规编译器之正式编译语
5 | *
6 | * @author 卢昶存
7 | */
8 |
9 | public interface Compile {
10 |
11 | /**
12 | * 若以此,需行标准之指令选项哉。
13 | * @param args 选项之令
14 | * @return 变数之阴阳
15 | */
16 | int compile(String... args);
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/lang/西曆法.wy:
--------------------------------------------------------------------------------
1 | 疏曰『
2 |
3 | 此經乃改編者以順java之道
4 |
5 | 參見 https://github.com/wenyan-lang/wenyan/tree/master/lib
6 |
7 |
8 | 』
9 |
10 | 今有一術。名之曰「紀元時」。是術曰。
11 | 施「{-> new Date().getTime()/1000}」。乃得矣。
12 | 是謂「紀元時」之術也。
13 |
14 | 今有一術。名之曰「時差」。欲行是術。必先得一數。曰「時」。乃行是術曰。
15 | 施「{x -> 0 - new Date(x * 1000).getTimezoneOffset() * 60}」於「時」。乃得矣。
16 | 是謂「時差」之術也。
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/lib/Defines.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.lib;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Target(ElementType.TYPE)
9 | @Retention(RetentionPolicy.RUNTIME)
10 | public @interface Defines {
11 |
12 | Define[] value();
13 | }
14 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | ## CONTRIBUTING NOTICE
2 |
3 | Wenyan and java enthusiasts are welcome to submit your code. If you are familiar with js, please turn to the wenyan project.
4 |
5 | If you need to provide your code, please ensure that the tests of the code are successful, and that the code is harmless and relevant to the meaning of the project.
6 |
7 | Please elaborate on its function or repair, so that we can refer to it.
8 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/LanguageCompiler.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler;
2 |
3 |
4 | /**
5 | * 万事成功皆包容,狭隘皆使万事崩。
6 | * 忆昔太宗盛名日,悲叹百年屈辱中。
7 | *
8 | * 故建此类包万物,万语皆可编译通。
9 | * 君仅继承此类者,并用语言类去行。
10 | *
11 | * 亦吾一词在心中,此器不只爪哇乎。
12 | */
13 | public interface LanguageCompiler {
14 |
15 | Class> compile(String script);
16 |
17 | Class> compile(String script, String className);
18 |
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/test/lang/wenyan/test/TestImport.java:
--------------------------------------------------------------------------------
1 | package lang.wenyan.test;
2 |
3 | import cn.wenyan.compiler.WenYanCompiler;
4 | import cn.wenyan.compiler.WenYanTools;
5 | import cn.wenyan.compiler.script.libs.Language;
6 | import org.junit.Test;
7 |
8 | public class TestImport {
9 |
10 | @Test
11 | public void test(){
12 | WenYanCompiler wenYanCompiler = WenYanTools.makeCompiler(Language.GROOVY);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/lang/畫譜.wy:
--------------------------------------------------------------------------------
1 |
2 |
3 | 吾有一列。名之曰「色表」。充「色表」
4 | 以「「黑」」。 以「「#000000」」。
5 | 以「「鈦白」」。 以「「#ffffff」」。
6 | 以「「藤黃」」。 以「「#faea56」」。
7 | 以「「硃磦」」。 以「「#d44906」」。
8 | 以「「硃砂」」。 以「「#c0927b」」。
9 | 以「「胭脂」」。 以「「#491817」」。
10 | 以「「曙紅」」。 以「「#c72c35」」。
11 | 以「「赭石」」。 以「「#522d14」」。
12 | 以「「大紅」」。 以「「#b91c1b」」。
13 | 以「「花青」」。 以「「#141931」」。
14 | 以「「三綠」」。 以「「#afceb5」」。
15 | 以「「酞青藍」」。 以「「#1e2867」」。
16 | 以「「三青」」。 以「「#acc8d4」」。
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/lib/Define.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.lib;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Target(ElementType.TYPE)
9 | @Retention(RetentionPolicy.RUNTIME)
10 | public @interface Define {
11 |
12 | String before();
13 |
14 | String after();
15 |
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/lib/WenYanException.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.lib;
2 |
3 | public class WenYanException extends Exception {
4 |
5 | private String name;
6 |
7 | public String getName() {
8 | return name;
9 | }
10 |
11 | public void setName(String name) {
12 | this.name = name;
13 | }
14 |
15 | @Override
16 | public String toString() {
17 | return name;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/plugins/Main.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.plugins;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Target(ElementType.TYPE)
9 | @Retention(RetentionPolicy.RUNTIME)
10 | public @interface Main {
11 |
12 | String name();
13 |
14 | Class> after();
15 |
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/WenYanCompiler.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler;
2 |
3 |
4 | public interface WenYanCompiler extends Compile{
5 |
6 | String ERROR = "error";
7 |
8 | String WYG = "藏書樓";
9 |
10 | String WYG_LIB = "序.wy";
11 |
12 | Class> compileToClass(String className,String... wenyanString);
13 |
14 | Class> compileToClass(String... wenyans);
15 |
16 | String dispatch(String wenyan);
17 |
18 | WenYanRuntime getRuntime();
19 | }
20 |
--------------------------------------------------------------------------------
/project_example/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cn.noyark
8 | project_example
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/lib/MathUtil.groovy:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.lib
2 |
3 | import groovy.transform.CompileStatic
4 |
5 | @CompileStatic
6 | class MathUtil {
7 |
8 | static Number mod(BigDecimal decimal1,BigDecimal decimal2){
9 | if(decimal1.toString().contains(".")||decimal2.toString().contains(".")){
10 | return decimal1
11 | }else{
12 | return decimal1.toBigInteger()%(decimal2.toBigInteger())
13 | }
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/plugins/Listener.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.plugins;
2 |
3 | import cn.wenyan.compiler.streams.CompileStream;
4 |
5 | import java.util.List;
6 |
7 | public abstract class Listener {
8 |
9 | public abstract void onCompileStart(CompileStream stream,List list);
10 |
11 | public abstract void onCompileFinish(CompileStream stream, List list);
12 |
13 | public abstract void onCompileFailed(CompileStream stream,List list);
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/log/Message.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.log;
2 |
3 | public class Message{
4 | String message;
5 | Throwable throwable;
6 |
7 | public Message(String message, Throwable throwable) {
8 | this.message = message;
9 | this.throwable = throwable;
10 | }
11 |
12 | public String getMessage() {
13 | return message;
14 | }
15 |
16 | public Throwable getThrowable() {
17 | return throwable;
18 | }
19 |
20 |
21 |
22 | }
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/GroovyCommand.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 |
4 | public class GroovyCommand extends Command {
5 |
6 | public GroovyCommand() {
7 | super("-g",0);
8 | }
9 |
10 | @Override
11 | public Object execute(String[] args, CompilerConfig compilerConfig) {
12 | compilerConfig.setGroovy(false);
13 | return null;
14 | }
15 |
16 | @Override
17 | public String help() {
18 | return "選擇生成groovy文件";
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/StrongTypeCommand.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 | public class StrongTypeCommand extends Command {
4 |
5 | public StrongTypeCommand() {
6 | super("-st",0);
7 | }
8 |
9 | @Override
10 | public Object execute(String[] args, CompilerConfig compilerConfig) {
11 | compilerConfig.setStrongType(true);
12 | return null;
13 | }
14 |
15 | @Override
16 | public String help() {
17 | return "开启强类型编译模式";
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/LexerViewerCommand.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 | public class LexerViewerCommand extends Command{
4 |
5 |
6 | public LexerViewerCommand(){
7 | super("-lv",0);
8 | }
9 |
10 | @Override
11 | public Object execute(String[] args, CompilerConfig compilerConfig) {
12 | compilerConfig.setLexerViewer(true);
13 | return null;
14 | }
15 |
16 | @Override
17 | public String help() {
18 | return "可視化分詞信息";
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/OutFileCommand.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 | public class OutFileCommand extends Command {
4 |
5 | public OutFileCommand() {
6 | super("-o",1);
7 | }
8 |
9 | @Override
10 | public Object execute(String[] args, CompilerConfig compilerConfig) {
11 | compilerConfig.setOutFile(args[0]);
12 | return null;
13 | }
14 |
15 | @Override
16 | public String help() {
17 | return "参数: OutFile,单独设置编译文件输出的文件夹";
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/PinYinCommand.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 | public class PinYinCommand extends Command {
4 |
5 | public PinYinCommand() {
6 | super("-p",1);
7 | }
8 |
9 | @Override
10 | public Object execute(String[] args, CompilerConfig compilerConfig) {
11 | compilerConfig.setSupportPinYin(Boolean.parseBoolean(args[0]));
12 | return null;
13 | }
14 |
15 | @Override
16 | public String help() {
17 | return "该指令已经废弃,使用无效";
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/lang/籌經.wy:
--------------------------------------------------------------------------------
1 | 注曰「「求和。同Javascript之reduce((a,b)=>a+b)也。」」
2 | 今有一術。名之曰「求和」。欲行是術。必先得一列。曰「列」。乃行是術曰。
3 | 吾有一數。曰零。名之曰「和」。凡「列」中之「數」。加「和」以「數」。昔之「和」者,今其是也。
4 | 乃得「和」。是謂「求和」之術也。
5 |
6 | 今有一術。名之曰「算術平均」。欲行是術。必先得一列。曰「列」。乃行是術曰。
7 | 施「求和」於「列」。名之曰「和」。夫「列」之長。名之曰「長」。除「和」以「長」。
8 | 名之曰「算術平均數」。乃得「算術平均數」。是謂「算術平均」之術也。
9 |
10 | 今有一術。名之曰「方差」。欲行是術。必先得一列。曰「列」。乃行是術曰。
11 | 施「算術平均」於「列」。名之曰「算術平均數」。夫「列」之長。名之曰「長」。
12 | 吾有一數。曰零。名之曰「平方和」。凡「列」中之「數」。減「數」以「算術平均數」。
13 | 名之曰「偏差」。乘「偏差」以「偏差」。名之曰「偏差之平方」。
14 | 加「偏差之平方」以「平方和」。昔之「平方和」者,今其是也。除「平方和」以「長」。
15 | 名之曰「方差之數」。乃得「方差之數」。是謂「方差」之術也。
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/SourceCommand.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 | public class SourceCommand extends Command{
4 |
5 |
6 | public SourceCommand() {
7 | super("-sc",1);
8 | }
9 |
10 | @Override
11 | public Object execute(String[] args, CompilerConfig compilerConfig) {
12 | compilerConfig.setSourcePath(args[0]);
13 | return null;
14 | }
15 |
16 | @Override
17 | public String help() {
18 | return "参数: sourcePath,指定源文件的根目录,根目录下面的文件夹则作为package名称";
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/WenYuanGeDownloadCommand.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 | public class WenYuanGeDownloadCommand extends Command {
4 |
5 | public WenYuanGeDownloadCommand() {
6 | super("-wd",1);
7 | }
8 |
9 | @Override
10 | public Object execute(String[] args, CompilerConfig compilerConfig) {
11 | compilerConfig.setWygDownload(args[0]);
12 | return null;
13 | }
14 |
15 | @Override
16 | public String help() {
17 | return "参数: name,相当于wyg i name,确保有node.js";
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/utils/ScalaUtils.scala:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.utils
2 |
3 | import cn.wenyan.compiler.WenYanLib
4 |
5 |
6 | object ScalaUtils {
7 |
8 | def containsCommonNumber(wenyan:String) : Boolean ={
9 | val numbers = WenYanLib.prefixs
10 | for((x,y)<-numbers){
11 | if(wenyan.contains(x))return true
12 | }
13 | false
14 | }
15 |
16 | def countTime(func : =>Unit): Long ={
17 | val start = System.currentTimeMillis
18 | func
19 | val end = System.currentTimeMillis
20 | return end -start
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/lang/物經.wy:
--------------------------------------------------------------------------------
1 | 疏曰『
2 |
3 | 此經乃改編者以順java之道
4 |
5 | 參見 https://github.com/wenyan-lang/wenyan/tree/master/lib
6 |
7 |
8 | 』
9 | 或云「「觀物「甲」為非數」」。
10 | 蓋謂「「施「{x -> x.class == Integer.class||x.class == BigInteger.class||x.class == java.math.BigDecimal}」於「甲」。」」
11 |
12 | 或云「「觀物「甲」為非列」」。
13 | 蓋謂「「施「{x -> x.class == JSArray.class}」於「甲」。」」
14 |
15 | 或云「「觀物「甲」為非言」」。
16 | 蓋謂「「施「{x -> x.class == String.class}」於「甲」。」」
17 |
18 | 或云「「觀物「甲」為非物」」。
19 | 蓋謂「「施「{x -> x.class == Map.class}」於「甲」。」」
20 |
21 | 或云「「觀物「甲」為非爻」」。
22 | 蓋謂「「施「{x -> x.class == Boolean.class}」於「甲」。」」
23 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/ClassFileCommand.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 | public class ClassFileCommand extends Command {
4 |
5 | public ClassFileCommand() {
6 | super("-n",1);
7 | }
8 |
9 | @Override
10 | public Object execute(String[] args, CompilerConfig compilerConfig) {
11 | compilerConfig.setClassFile(args[0]);
12 | return null;
13 | }
14 |
15 | @Override
16 | public String help() {
17 | return "参数: 主类,运行时,设置运行主类,比如cn.main.HelloWorld,对应着@sourceFile/cn/main/HelloWorld.wy";
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/MainClassCommand.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 | public class MainClassCommand extends Command {
4 |
5 | public MainClassCommand() {
6 | super("-m", 1);
7 | }
8 |
9 | @Override
10 | public Object execute(String[] args, CompilerConfig compilerConfig) {
11 | compilerConfig.setMainClass(args[0]);
12 | return null;
13 | }
14 |
15 | @Override
16 | public String help() {
17 | return "参数: 主类,编译时,设置编译主类,比如cn.main.HelloWorld,对应着@sourceFile/cn/main/HelloWorld.wy";
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/CompileLibCommand.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 | public class CompileLibCommand extends Command {
4 |
5 | public CompileLibCommand() {
6 | super("-l",1);
7 | }
8 |
9 | @Override
10 | public Object execute(String[] args, CompilerConfig compilerConfig) {
11 | String[] libs = args[0].split(";");
12 | compilerConfig.setCompileLib(libs);
13 | return null;
14 | }
15 |
16 | @Override
17 | public String help() {
18 | return "参数: jarFiles,加入依赖的jar包,如hello.jar;eat.jar";
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/RunCommand.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 | public class RunCommand extends Command {
4 |
5 | public RunCommand() {
6 | super("-r",-1);
7 | }
8 |
9 | @Override
10 | public Object execute(String[] args, CompilerConfig compilerConfig) {
11 | compilerConfig.setRun(true);
12 | compilerConfig.setRunArgs(args);
13 | return null;
14 | }
15 |
16 | @Override
17 | public String help() {
18 | return "参数: 程序参数,运行主文件,-r后面跟着args,即程序参数,这个选项必须放在最后面,比如 -o xxx -c xxx -r 1 2 3";
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/lang/易經.wy:
--------------------------------------------------------------------------------
1 | 疏曰『
2 |
3 | 此經乃改編者以順java之道
4 |
5 | 參見 https://github.com/wenyan-lang/wenyan/tree/master/lib
6 |
7 |
8 | 』
9 | 吾有一數。曰四十二。名之曰「運數」。
10 |
11 | 今有一術。名之曰「運」。欲行是術。必先得一數。曰「甲」。乃行是術曰。
12 | 注曰「「運者。隨機種子也」」
13 | 昔之「運數」者。今「甲」是矣。
14 | 是謂「運」之術也。
15 |
16 | 今有一術。名之曰「占」。是術曰。
17 | 注曰「「線性同餘方法所得隨機數也」」
18 | 有數四十二億九千四百九十六萬七千二百九十六。名之曰「模」。
19 | 注曰「「有數二千二百六十九萬五千四百七十七。名之曰「倍」。」」
20 | 有數二千二百六十七萬五千四百五十六。名之曰「上倍」。有數二萬零二十一。名之曰「下倍」。
21 | 有數一。名之曰「增」。
22 | 乘「上倍」於「運數」。除其以「模」。所餘幾何。名之曰「上餘」。
23 | 乘「下倍」於「運數」。加其於「上餘」。加其以「增」。除其以「模」。所餘幾何。昔之「運數」者。今其是矣。
24 | 除「運數」以「模」。名之曰「卦」。
25 | 乃得「卦」。
26 | 是謂「占」之術也。
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/WenYuanGeCommand.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 | import cn.wenyan.compiler.WenYanCompiler;
4 |
5 | public class WenYuanGeCommand extends Command{
6 |
7 | public WenYuanGeCommand() {
8 | super("-wyg",1);
9 | }
10 |
11 | @Override
12 | public Object execute(String[] args, CompilerConfig compilerConfig) {
13 | compilerConfig.setWenyuangeFile(args[0]);
14 | return null;
15 | }
16 |
17 | @Override
18 | public String help() {
19 | return "参数: "+WenYanCompiler.WYG+"的上一级目录,将文渊阁编译进去,后面跟着"+ WenYanCompiler.WYG+"的上一级目录即可,需要有-sc和-o的内容";
20 | }
21 | }
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/Command.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 | public abstract class Command {
4 |
5 | private String option;
6 |
7 | private int argsLength;
8 |
9 | public abstract Object execute(String[] args,CompilerConfig compilerConfig);
10 |
11 | public Command(String option, int argsLength) {
12 | this.option = option;
13 | this.argsLength = argsLength;
14 | }
15 |
16 |
17 | public int getArgsLength() {
18 | return argsLength;
19 | }
20 |
21 | public String getOption() {
22 | return option;
23 | }
24 |
25 | public abstract String help();
26 | }
27 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
21 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/exceptions/CommandException.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.exceptions;
2 |
3 | public class CommandException extends RuntimeException {
4 |
5 | public CommandException() {
6 | }
7 |
8 | public CommandException(String message) {
9 | super(message);
10 | }
11 |
12 | public CommandException(String message, Throwable cause) {
13 | super(message, cause);
14 | }
15 |
16 | public CommandException(Throwable cause) {
17 | super(cause);
18 | }
19 |
20 | public CommandException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
21 | super(message, cause, enableSuppression, writableStackTrace);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/utils/ResultEntry.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.utils;
2 |
3 | import java.util.List;
4 |
5 | public class ResultEntry {
6 |
7 | private List code;
8 |
9 | private String outCode;
10 |
11 | public ResultEntry(List code, String outCode) {
12 | this.code = code;
13 | this.outCode = outCode;
14 | }
15 |
16 | public List getCode() {
17 | return code;
18 | }
19 |
20 | public void setCode(List code) {
21 | this.code = code;
22 | }
23 |
24 | public String getOutCode() {
25 | return outCode;
26 | }
27 |
28 | public void setOutCode(String outCode) {
29 | this.outCode = outCode;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/utils/SyntaxColor.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.utils;
2 |
3 | import cn.wenyan.compiler.WenYanCompilerImpl;
4 |
5 | public class SyntaxColor {
6 |
7 |
8 | public static String[] command = new String[]{
9 | "吾","有","曰","名","書","數","言","爻","列","物",
10 | "昔","者","今","是","疏","注","批","為",
11 | "遍","云","也","若","者","止","恆","若非",
12 | "加","減","乘","除","以","於","矣","所餘幾何","夫",
13 | "有","陽","無","陰","乎","中","凡","中","術","術",
14 | "得","謂","術","欲行是術","必先","行","施",
15 | "於","嘗觀","書","方悟","義","是","乃","之","欲"
16 | };
17 | public static String getSyntaxColor(String wenyan, WenYanCompilerImpl compiler){
18 | return wenyan;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/WenYanTools.groovy:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler
2 |
3 | import cn.wenyan.compiler.script.libs.Language
4 | import groovy.transform.CompileStatic
5 | import groovy.transform.TypeCheckingMode
6 |
7 | @CompileStatic
8 | class WenYanTools {
9 |
10 | static {
11 | setDefault()
12 | }
13 |
14 | @CompileStatic(TypeCheckingMode.SKIP)
15 | private static void setDefault(){
16 | WenYanTools.metaClass.static.default = {
17 | getDefault()
18 | }
19 | }
20 |
21 | static WenYanCompiler makeCompiler(Language language){
22 | return new WenYanCompilerImpl(false,language)
23 | }
24 |
25 | static WenYanCompiler getDefault(){
26 | return makeCompiler(Language.GROOVY)
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/utils/FileFormat.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.utils;
2 |
3 | import org.apache.commons.io.FileUtils;
4 |
5 | import java.io.File;
6 | import java.io.IOException;
7 | import java.util.List;
8 |
9 | public class FileFormat {
10 |
11 | public List formatEnd(File file){
12 | try {
13 | List strings = FileUtils.readLines(file, System.getProperty("file.coding"));
14 | for(int i = 0;i streams = new ArrayList<>();
14 |
15 | private WenYanCompilerImpl compiler;
16 |
17 | public StreamBuilder(WenYanCompilerImpl compiler){
18 | this.compiler = compiler;
19 | }
20 |
21 | public StreamBuilder put(CompileStream stream){
22 | streams.add(stream);
23 | return this;
24 | }
25 |
26 | public CompileFactory build(){
27 | return new CompileFactory(streams,compiler);
28 | }
29 |
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/utils/SpecialSyntaxLexer.scala:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.utils
2 |
3 | import cn.wenyan.compiler.WenYanLib
4 |
5 | object SpecialSyntaxLexer {
6 |
7 |
8 | def matches(p : (String,String),index : Int,strings: String,patterns : scala.collection.mutable.Map[String,String]) : Boolean={
9 | if(p._2.equals(patterns(WenYanLib.STATEMENT))){
10 | if(index+1<=strings.length-1&&strings(index+1) == '之')return false
11 | }
12 | if(p._2.equals(patterns(WenYanLib.GET))){
13 | if(index+1<=strings.length-1&&strings(index+1) == '餘')return false
14 | }
15 | if(p._2.equals(patterns(WenYanLib.AND_OR))){
16 | if(index+1<=strings.length-1&&strings(index+1) == '「')return false
17 | }
18 | true
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/utils/Assert.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.utils;
2 |
3 | import cn.wenyan.compiler.WenYanCompilerImpl;
4 | import cn.wenyan.compiler.exceptions.SyntaxException;
5 |
6 | import java.util.List;
7 |
8 | public class Assert {
9 |
10 | public static void typeCheck(WenYanCompilerImpl stream, String name, String code, String... types){
11 | String type = stream.getNameType().get(name);
12 | for(String s: types){
13 | if(type.equals(s)){
14 | throw new SyntaxException("昔"+name+"为"+type+"者,不能迭代之: "+code);
15 | }
16 | }
17 | }
18 |
19 | public static List syntaxError(String wenyan) throws SyntaxException{
20 | String prefix = "\n于此言有误: ";
21 | throw new SyntaxException(prefix+ wenyan+"\n");
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/plugins/JavaPluginClassloader.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.plugins;
2 |
3 | import java.io.File;
4 | import java.net.MalformedURLException;
5 | import java.net.URL;
6 | import java.net.URLClassLoader;
7 |
8 | public class JavaPluginClassloader extends URLClassLoader {
9 |
10 | public JavaPluginClassloader(File file) throws MalformedURLException {
11 | super(new URL[]{file.toURI().toURL()}, JavaPluginClassloader.class.getClassLoader());
12 | }
13 |
14 | @Override
15 | protected Class> loadClass(String name, boolean resolve) throws ClassNotFoundException {
16 | if(name.startsWith("cn.wenyan")){
17 | throw new ClassNotFoundException("you couldn't use cn.wenyan package name");
18 | }
19 | return super.loadClass(name, resolve);
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/test/TestMain.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.test;
2 |
3 | import cn.wenyan.compiler.WenYanCompiler;
4 | import cn.wenyan.compiler.WenYanTools;
5 | import cn.wenyan.compiler.script.libs.Language;
6 |
7 | public class TestMain {
8 | //
9 | public static void main(String[] args) {
10 | String project = "/Users/luchangcun/Projects/new/wenyan-jvm/project_example";
11 | String makeFile = project+"/MakeFile.txt";
12 | String sc = project+"/src/main/java";
13 | String out = project+"/target";
14 | WenYanCompiler impl = WenYanTools.makeCompiler(Language.GROOVY);
15 | impl.compile("-c","@"+makeFile,out,"-sc",sc,"-m","main.主文件","-g","-wyg",sc);
16 | impl.compile("-o","/Users/luchangcun/Projects/new/wenyan-jvm/project_example/target/","-n","main.主文件","-r");
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/CompileBackend.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler;
2 |
3 |
4 | import java.io.File;
5 | import java.io.FileNotFoundException;
6 | import java.util.List;
7 |
8 | public interface CompileBackend extends Init{
9 |
10 | void appendClassName(int index, String mainClass, String className, List codes, StringBuilder builder, String annotation, String pack);
11 |
12 | default String filterAndToString(List results,String filter){
13 | StringBuilder builder = new StringBuilder();
14 | for(String r :results) {
15 | if(r!=null&&filter!=null&&r.startsWith(filter))continue;
16 | builder.append("\n").append(r);
17 | }
18 | return builder.toString();
19 | }
20 |
21 |
22 |
23 | void compileToClass(File out, File classFile) throws FileNotFoundException;
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/plugins/Plugin.scala:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.plugins
2 |
3 | import java.util.regex.Pattern
4 |
5 | import cn.wenyan.compiler.streams.CompileStream
6 |
7 | import scala.collection.mutable
8 | import java.util
9 |
10 | import cn.wenyan.compiler.Init
11 |
12 | abstract class Plugin extends Init{
13 |
14 | def addSyntaxRegex(map : mutable.Map[String,String])
15 |
16 | def addPatterns(map : mutable.Map[String,Pattern])
17 |
18 | def addCompileStream(map : util.List[CompileStream])
19 |
20 | def addListener(map : util.List[Listener])
21 |
22 | //如果你需要拦截高优先级的语法,则可以在这里拦截,可以参见
23 | //LexerUtils的canMatch中的三个if判断
24 | //如语法有夫「x」之长和夫「x」,为了防止分割夫「x」之长,可以在匹配夫「x」时
25 | //后面有没有之,来组合上面的语法
26 | def onCanMatch(p : (String,String),index : Int,strings: String,patterns : util.Map[String,String]) : Boolean
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/start/wenyan.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | #######
4 | #1. wenyan.sh compile @MakeFile.wy targetPath sourcePath mainClass
5 | #2. wenyan.sh wyg targetPath sourcePath
6 | #3. wenyan,sh run targetPath mainClass args
7 | #4. wenyan.sh help
8 | #5. wenyan.sh common args
9 | #######
10 |
11 | compile='compile'
12 | wyg='wyg'
13 | run='run'
14 | help='help'
15 | common='common'
16 | c="java -jar wenyan.jar"
17 | if [ "$1" == $compile ]; then
18 |
19 | "$c" -c $2 $3 -sc $4 -m $5
20 |
21 | elif [ "$1" == $wyg ]; then
22 |
23 | "$c" -o $1 -sc $2 -wyg $2
24 |
25 | elif [ "$1" == $run ]; then
26 | ar="$4"
27 | for (( i = 5; i <= $#; ++i )); do
28 | ar+=" ${!i}"
29 | done
30 | "$c" -o $2 -n $3 -r "$ar"
31 | elif [ "$1" == $help ]; then
32 | java -jar wenyan.jar
33 | elif [ "$1" == $common ]; then
34 | ar=""
35 | for (( i = 0; i <= $#; ++i )); do
36 | ar+=" ${!i}"
37 | done
38 | "$c" "$ar"
39 | fi
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/streams/CompileStream.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.streams;
2 |
3 | import cn.wenyan.compiler.CompileResult;
4 | import cn.wenyan.compiler.WenYanCompilerImpl;
5 | import cn.wenyan.compiler.script.libs.Language;
6 | import cn.wenyan.compiler.script.libs.Library;
7 |
8 | import java.util.List;
9 |
10 | public abstract class CompileStream {
11 |
12 | protected WenYanCompilerImpl compiler;
13 |
14 | protected Language language;
15 |
16 | protected Library library;
17 |
18 | CompileStream(WenYanCompilerImpl compiler){
19 | this.compiler = compiler;
20 | this.language = compiler.getLanguageType();
21 | this.compiler.getStreamMap().put(this.getClass(),this);
22 | this.library = compiler.getLibrary();
23 | }
24 | public abstract CompileResult compile(List wenyan);
25 |
26 | public Language getLanguage() {
27 | return language;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/WenYanRuntime.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler;
2 |
3 | import java.io.File;
4 | import java.io.IOException;
5 |
6 | public class WenYanRuntime {
7 |
8 | private WenYanCompilerImpl compiler;
9 |
10 | private WenYanShell shell;
11 |
12 | public WenYanRuntime(WenYanCompilerImpl compiler){
13 | this(compiler,new WenYanShell(compiler));
14 | }
15 |
16 | public WenYanRuntime(WenYanCompilerImpl compiler,WenYanShell shell){
17 | this.compiler = compiler;
18 | this.shell = shell;
19 | }
20 |
21 | public void runFile(String file){
22 | try {
23 | runFile(new File(file));
24 | }catch (IOException e){
25 | compiler.getServerLogger().info("",e);
26 | }
27 | }
28 |
29 |
30 | public WenYanShell getShell() {
31 | return shell;
32 | }
33 |
34 | public void runFile(File file) throws IOException {
35 | shell.run(false,compiler.getWenYanCodeByFile(file));
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior:
15 | 1. Go to '...'
16 | 2. Click on '....'
17 | 3. Scroll down to '....'
18 | 4. See error
19 |
20 | **Expected behavior**
21 | A clear and concise description of what you expected to happen.
22 |
23 | **Screenshots**
24 | If applicable, add screenshots to help explain your problem.
25 |
26 | **Desktop (please complete the following information):**
27 | - OS: [e.g. iOS]
28 | - Browser [e.g. chrome, safari]
29 | - Version [e.g. 22]
30 |
31 | **Smartphone (please complete the following information):**
32 | - Device: [e.g. iPhone6]
33 | - OS: [e.g. iOS8.1]
34 | - Browser [e.g. stock browser, safari]
35 | - Version [e.g. 22]
36 |
37 | **Additional context**
38 | Add any other context about the problem here.
39 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/test/PackStdLib.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.test;
2 |
3 | import cn.wenyan.compiler.WenYanCompilerImpl;
4 | import cn.wenyan.compiler.WenYanTools;
5 | import cn.wenyan.compiler.script.libs.Language;
6 |
7 | import java.io.File;
8 |
9 | public class PackStdLib {
10 | //施「JSON.stringify」於「太白餛飩」。書之。
11 | //施「包渾沌」於「太白餛飩」。書之。
12 | public static void main(String[] args) {
13 | String project = "/Users/luchangcun/Projects/new/wenyan-jvm/";
14 | WenYanCompilerImpl wenYanCompiler = (WenYanCompilerImpl) WenYanTools.makeCompiler(Language.GROOVY);
15 | String source = project+"src/main/java/";
16 | wenYanCompiler.getLibrary().getLibs().forEach((x,y)-> {
17 | wenYanCompiler
18 | .compile(
19 | "-c", source + y.replace(".", File.separator) + ".wy",
20 |
21 | project + "/target/classes",
22 |
23 | "-sc", source,"-g"
24 | );
25 | });
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/log/ILogger.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.log;
2 |
3 |
4 | /**
5 | * 接口
6 | */
7 | public interface ILogger {
8 |
9 | void emergency(String message);
10 |
11 | void alert(String message);
12 |
13 | void critical(String message);
14 |
15 | void error(String message);
16 |
17 | void warning(String message);
18 |
19 | void notice(String message);
20 |
21 | void info(String message);
22 |
23 | void debug(String message);
24 |
25 | void log(LogLevel level, String message);
26 |
27 | void emergency(String message, Throwable t);
28 |
29 | void alert(String message, Throwable t);
30 |
31 | void critical(String message, Throwable t);
32 |
33 | void error(String message, Throwable t);
34 |
35 | void warning(String message, Throwable t);
36 |
37 | void notice(String message, Throwable t);
38 |
39 | void info(String message, Throwable t);
40 |
41 | void debug(String message, Throwable t);
42 |
43 | void log(LogLevel level, String message, Throwable t);
44 | }
45 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 MagicLu
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/lang/位經.wy:
--------------------------------------------------------------------------------
1 | 疏曰『
2 |
3 | 此經乃改編者以順java之道
4 |
5 | 參見 https://github.com/wenyan-lang/wenyan/tree/master/lib
6 |
7 |
8 | 』
9 | 或云「「左移「甲」以「乙」」」。
10 | 蓋謂「「施「左移」於「甲」。於「乙」」」。
11 |
12 | 或云「「右移「甲」以「乙」」」。
13 | 蓋謂「「施「右移」於「甲」。於「乙」」」。
14 |
15 | 今有一術。名之曰「左移」。欲行是術。必先得二數。曰「甲」。曰「乙」。乃行是術曰。
16 | 施「{x,y -> x< x>>y}」於「甲」。於「乙」。名之曰「丙」。乃得「丙」。
21 | 是謂「右移」之術也。
22 |
23 | 今有一術。名之曰「補零右移」。欲行是術。必先得二數。曰「甲」。曰「乙」。乃行是術曰。
24 | 施「{x,y-> (x % 0x100000000) >> y}」於「甲」。於「乙」。名之曰「丙」。乃得「丙」。
25 | 是謂「補零右移」之術也。
26 |
27 | 今有一術。名之曰「位与」。欲行是術。必先得二數。曰「甲」。曰「乙」。乃行是術曰。
28 | 施「{x,y-> x&y}」於「甲」。於「乙」。名之曰「丙」。乃得「丙」。
29 | 是謂「位与」之術也。
30 |
31 | 今有一術。名之曰「位或」。欲行是術。必先得二數。曰「甲」。曰「乙」。乃行是術曰。
32 | 施「{x,y -> x|y}」於「甲」。於「乙」。名之曰「丙」。乃得「丙」。
33 | 是謂「位或」之術也。
34 |
35 | 今有一術。名之曰「异或」。欲行是術。必先得二數。曰「甲」。曰「乙」。乃行是術曰。
36 | 施「{x,y -> x^y}」於「甲」。於「乙」。名之曰「丙」。乃得「丙」。
37 | 是謂「异或」之術也。
38 |
39 | 今有一術。名之曰「与非」。欲行是術。必先得二數。曰「甲」。曰「乙」。乃行是術曰。
40 | 施「{x,y-> ~(x&y)}」於「甲」。於「乙」。名之曰「丙」。乃得「丙」。
41 | 是謂「与非」之術也。
42 |
43 | 今有一術。名之曰「位變」。欲行是術。必先得一數。曰「甲」。乃行是術曰。
44 | 施「{x-> ~x}」於「甲」。名之曰「乙」。乃得「乙」。
45 | 是謂「位變」之術也。
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/log/LogLevel.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.log;
2 |
3 |
4 | import java.util.function.BiConsumer;
5 |
6 |
7 | public enum LogLevel {
8 |
9 | NONE((Message m, ILogger logger)->{}),
10 | EMERGENCY((Message m, ILogger logger)->logger.emergency(m.message,m.throwable)),
11 | ALERT((Message m, ILogger logger)->logger.alert(m.message,m.throwable)),
12 | CRITICAL((Message m, ILogger logger)->logger.critical(m.message,m.throwable)),
13 | ERROR((Message m, ILogger logger)->logger.critical(m.message,m.throwable)),
14 | WARNING((Message m, ILogger logger)->logger.warning(m.message,m.throwable)),
15 | NOTICE((Message m, ILogger logger)->logger.notice(m.message,m.throwable)),
16 | INFO((Message m, ILogger logger)->logger.info(m.message,m.throwable)),
17 | DEBUG((Message m, ILogger logger)->logger.debug(m.message,m.throwable));
18 |
19 | private BiConsumer func;
20 |
21 | LogLevel(BiConsumer func) {
22 | this.func = func;
23 | }
24 |
25 | public BiConsumer getFunc() {
26 | return func;
27 | }
28 |
29 |
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/book/var.md:
--------------------------------------------------------------------------------
1 | # 关于文言文编程语言的变量
2 |
3 | > 类型
4 |
5 | |类型|解释|字面量例子|
6 | |---|---|---|
7 | |`數`|`number`类型,数字类型,可以是浮点型或者整型|`三千二百零一`,`二〇二〇`|
8 | |`言`|`string`类型,是字符串类型,可以是空字符串|`『予观夫巴陵胜状』`,`「「君子好逑」」`
9 | |`爻`|`boolean`类型,是布尔类型,只有两个值|`陰`,`陽`|
10 | |`列`|`array`类型,是数组类型|`...`|
11 | |`物`|`object`类型,定义对象|`...`|
12 | |`元`|`auto type`,可以反推类型|`...`|
13 |
14 | > 声明变量的表达式
15 |
16 | 通用表达式:
17 |
18 | 吾有\[N\]\[type\]。曰\[value1\]。曰\[value2\]...曰\[valueN\]。名之曰\[name1\]曰\[name2\]....\[nameN\]
19 |
20 | ###### 通用例子
21 |
22 | 1. 數 言 爻
23 | * 今/吾有一數/言/爻,曰三/「「问天地好在」」/陰,名之曰「甲」
24 | * 吾有三數。曰一。曰三。曰五。名之曰「甲」曰「乙」曰「丙」。
25 | 2. 元
26 | * 今/吾有一元,曰三/「「问天地好在」」/陰,名之曰「甲」
27 | 3. 列
28 | * 今/吾有一列,曰「甲」。充「甲」以四。以二。
29 | 4. 物
30 | * 今/吾有一物。名之曰「甲」。其物如是。物之「「乙」」者。數曰三。物之「「丙」」者。言曰「「丁」」。是謂「甲」之物也
31 |
32 | PS: 今关键字在javascript编译器中是编译为var name = this.name = value,java版编译器暂时没有做处理
33 |
34 | ###### 简化写法
35 |
36 | 有\[type\]\[value\]。名之曰\[name\]
37 |
38 | 有數五十。名之曰「大衍」。
39 |
40 | > 替换
41 |
42 | 昔之\[before\],今\[after\]是也/矣
43 |
44 | 译作: before = after
45 |
46 | PS: `也`一般作为`若非`和`若....者`的结束标志,如果不结束代码块,请使用`矣`
47 |
48 | > 其他
49 |
50 | 您可以可以在初始化的时候使用其他变量,如
51 |
52 | 吾有一數,曰「甲」,名之曰「乙」
53 |
54 | 译作: var 乙 = 甲;
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/lang/格物.wy:
--------------------------------------------------------------------------------
1 | 疏曰『
2 |
3 | 此經乃改編者以順java之道
4 |
5 | 參見 https://github.com/wenyan-lang/wenyan/tree/master/lib
6 |
7 |
8 | 』
9 | 今有一術。名之曰「取物」。欲行是術。必先得一物曰「物」。一言曰「端」。乃行是術曰。
10 | 施「{ obj,x -> obj[x]}」於「物」於「端」。乃得矣。
11 | 是謂「取物」之術也。
12 |
13 | 今有一術。名之曰「置物」。欲行是術。必先得一物曰「物」。一言曰「端」。一元曰「實」。乃行是術曰。
14 | 施「{obj,x,v -> obj[x] = v}」於「物」於「端」於「實」。
15 | 是謂「置物」之術也。
16 |
17 | 今有一術。名之曰「列物之端」。欲行是術。必先得一物曰「物」。乃行是術曰。
18 | 施「{x -> ArrayUtils.toJSArray(x).keys()}」於「物」乃得矣。
19 | 是謂「列物之端」之術也。
20 |
21 | 今有一術。名之曰「識類」。欲行是術。必先得一元曰「元」。乃行是術曰。
22 | 施「{
23 | x->
24 | switch(x.getClass().getName()){
25 | case "java.lang.Integer":
26 | case "java.math.BigInteger":
27 | case "java.math.BigDecimal":
28 | return "數"
29 | case "cn.wenyan.compiler.lib.JSArray":
30 | return "列"
31 | case "java.lang.String":
32 | return "言"
33 | case "java.lang.Boolean":
34 | return "爻"
35 | case "java.util.LinkedHashMap":
36 | return "物"
37 | default:
38 | return x.getClass().getName()
39 | }
40 | }」於「元」乃得矣。
41 | 是謂「識類」之術也。
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/CompileFileCommand.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 | import org.apache.commons.io.FileUtils;
4 |
5 | import java.io.File;
6 | import java.io.IOException;
7 |
8 | public class CompileFileCommand extends Command {
9 |
10 | public CompileFileCommand() {
11 | super("-c",2);
12 | }
13 |
14 | @Override
15 | public Object execute(String[] args, CompilerConfig compilerConfig) {
16 | String name = args[0];
17 | if(name.startsWith("@")){
18 | name = name.substring(name.indexOf("@")+1);
19 | try {
20 | compilerConfig.setCompileFiles(FileUtils.readLines(new File(name), System.getProperty("file.codings")).toArray(new String[0]));
21 | }catch (IOException e){
22 | System.err.println("-----> No Such File");
23 | }
24 | }else{
25 | compilerConfig.setCompileFiles(name);
26 | }
27 | compilerConfig.setOutFile(args[1]);
28 | return null;
29 | }
30 |
31 | @Override
32 | public String help() {
33 | return "参数: makeFile和outFile,设置编译文件路径,如@MakeFile.txt或者HelloWorld.wy,后面是输出的文件夹根目录";
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/CompileResult.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler;
2 |
3 | import cn.wenyan.compiler.utils.Utils;
4 |
5 |
6 | import java.util.Collections;
7 | import java.util.List;
8 |
9 | /**
10 | * 欲语此类用者何,成名一诗少不了。
11 | *
12 | * 若使君知何成者,成果皆或败事否。
13 | * 功成不少家书至,故以两者记事和。
14 | *
15 | * 此用流程以明理,成功之字以终行。
16 | * 此用流程以明志,结果之符以结行。
17 | *
18 | * 春风君者何时至,红灯不删绿灯鸣。
19 | */
20 | public class CompileResult {
21 |
22 | private boolean success;
23 |
24 | private List result;
25 |
26 | public CompileResult(boolean success, List result) {
27 | this.success = success;
28 | this.result = result;
29 | }
30 |
31 | public CompileResult(String result){
32 | this.success = true;
33 | this.result = Collections.singletonList(result);
34 | }
35 |
36 | public List getResult() {
37 | return result;
38 | }
39 |
40 | public boolean isSuccess() {
41 | return success;
42 | }
43 |
44 | public void setResult(List result) {
45 | this.result = result;
46 | }
47 |
48 | @Override
49 | public String toString() {
50 | return Utils.getWenyanFromArray(result.toArray(new String[0]));
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/GroovyCompiler.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler;
2 |
3 | import groovy.lang.GroovyClassLoader;
4 |
5 | /**
6 | *
7 | * 欲语此类用者何,成名一诗少不了。
8 | *
9 | * 若以成者行此中,必先编译以横行。
10 | * 此类不出实体者,即编即用以交互。
11 | *
12 | * 先得加载对象者,终以源码得英雄,
13 | * 此类内涵不言耳,故以工具以敝之。
14 | *
15 | */
16 | public class GroovyCompiler implements LanguageCompiler{
17 |
18 | /**
19 | * 获取脚本的class
20 | * @param script
21 | * @return
22 | */
23 | public Class> compile(String script) {
24 | GroovyClassLoader loader = getDefaultParentGroovyClassLoader();
25 | return loader.parseClass(script);
26 | }
27 |
28 | /**
29 | * 获取脚本的class对象
30 | * @param script groovy 脚本
31 | * @param className 类名字
32 | */
33 | public Class> compile(String script, String className) {
34 | GroovyClassLoader loader = getDefaultParentGroovyClassLoader();
35 | return loader.parseClass(script, className);
36 | }
37 |
38 | /**
39 | * 默认的父加载器为GroovyCompiler().getClass().getClassLoader()
40 | * @return 返回groovy的类加载器
41 | */
42 | private GroovyClassLoader getDefaultParentGroovyClassLoader() {
43 | ClassLoader cl = GroovyCompiler.class.getClassLoader();
44 | return new GroovyClassLoader(cl);
45 | }
46 |
47 |
48 | }
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/utils/GroovyPrettyCode.groovy:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.utils
2 |
3 | import groovy.transform.CompileStatic;
4 |
5 | @CompileStatic
6 | class GroovyPrettyCode implements PrettyCode {
7 |
8 | @Override
9 | String pretty(String string) {
10 | StringBuilder builder = new StringBuilder()
11 | int close = 0 //检验是否在字符串里
12 | //字符串的数量%2 == 0
13 | List l = GroovyUtils.splitGroovyCode(string,"\n")
14 | for(String code in l){
15 | int clo = getClose(code)
16 | if(clo < 0){
17 | close += clo
18 | }
19 | builder.append("\t"*close).append(code).append("\n")
20 | if(clo > 0){
21 | close += clo
22 | }
23 | }
24 | return builder
25 | }
26 |
27 | private static int getClose(String code){
28 | int close = 0
29 | int string = 0
30 | for(s in code){
31 | if(s == "'"||s == "\"") {
32 | string ++
33 | }
34 | if(string %2 == 0){
35 | if(s == "{"){
36 | close ++
37 | }
38 | if(s == "}"){
39 | close --
40 | }
41 | }
42 | }
43 | close
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/script/libs/Library.groovy:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.script.libs
2 |
3 | import groovy.transform.CompileStatic
4 |
5 | @CompileStatic
6 | class Library {
7 |
8 | private Language language
9 |
10 | Library(Language language) {
11 | this.language = language
12 | }
13 |
14 | private final Map> libs = [
15 | (Language.GROOVY) : [
16 | "算經" : "cn.wenyan.lang.算經",
17 | "位經" : "cn.wenyan.lang.位經",
18 | "列經" : "cn.wenyan.lang.列經",
19 | "物經" : "cn.wenyan.lang.物經",
20 | "易經" : "cn.wenyan.lang.易經",
21 | "格物" : "cn.wenyan.lang.格物",
22 | "渾沌經":"cn.wenyan.lang.渾沌經",
23 | "曆法":"cn.wenyan.lang.曆法",
24 | "曆表":"cn.wenyan.lang.曆表",
25 | "西曆法":"cn.wenyan.lang.西曆法",
26 | "籌經":"cn.wenyan.lang.籌經",
27 | "畫譜":"cn.wenyan.lang.畫譜"
28 | ]
29 | ]
30 |
31 | Map getLibs(){
32 | return libs.get(language)
33 | }
34 |
35 | void addLib(String name,String packageName){
36 | libs.get(language).put(name,packageName)
37 | }
38 |
39 | String get(String name){
40 | String n = getLibs().get(name)
41 | return n == null?name:n
42 | }
43 | }
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/streams/CommentCompileStream.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.streams;
2 |
3 | import cn.wenyan.compiler.CompileResult;
4 | import cn.wenyan.compiler.WenYanCompilerImpl;
5 | import cn.wenyan.compiler.WenYanLib;
6 | import cn.wenyan.compiler.script.libs.LanguageUtils;
7 | import cn.wenyan.compiler.utils.Utils;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * 此为批注之编译流。亦可编译"注释"之术,曰: 经必有注也。
13 | * 批曰。「「文氣淋灕。字句切實」」。
14 | * 注曰。「「文言備矣」」。
15 | * 疏曰。「「居第一之位故稱初。以其陽爻故稱九」」。
16 | * 此术数亦在运行时忽略之。
17 | *
18 | * @author MagicLu
19 | * @see CompileStream
20 | */
21 | public class CommentCompileStream extends CompileStream {
22 |
23 |
24 | public CommentCompileStream(WenYanCompilerImpl compiler) {
25 | super(compiler);
26 | }
27 |
28 | /**
29 | * 译之,若不成,则予下一流,反则为原文者乎
30 | * @param wenyan 上一流之返回者,亦为尚否编译者也
31 | * @return 编译之果
32 | */
33 | @Override
34 | public CompileResult compile(List wenyan) {
35 | if(Utils.matches(wenyan, WenYanLib.NEW_COMMENT())){
36 | compiler.removeWenyan();
37 | String value02 = compiler.removeWenyan();
38 | return new CompileResult(LanguageUtils.addComment(language,
39 | Utils.getStringFrom(WenYanLib.COMMENT(),value02,WenYanLib.STRING_START(),WenYanLib.STRING_END())
40 |
41 | ));
42 | }
43 | return new CompileResult(false,wenyan);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/lib/ArrayUtils.groovy:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.lib
2 |
3 | class ArrayUtils {
4 | static def getIndex(index){
5 | if(index instanceof Integer)return (Integer)index - 1
6 | if(index instanceof BigInteger)return (BigInteger)index -1
7 | return index
8 | }
9 |
10 | static def getArray(array){
11 | return array.getClass() == HashMap.Node.class?array.getValue():array
12 | }
13 |
14 | static JSArray toJSArray(array){
15 | if(array instanceof JSArray){
16 | return array
17 | }
18 | JSArray js = new JSArray()
19 | for(v in array){
20 | js.add(v)
21 | }
22 | return js
23 | }
24 |
25 | static def slice(str){
26 | if(str.getClass().isArray()){
27 | def arr = Arrays.asList(str)
28 | return arr.subList(1,arr.size())
29 | }
30 | if(str instanceof List){
31 | def list = new ArrayList(str)
32 | return new ArrayList<>(list.subList(1,list.size()))
33 | }
34 | if(str instanceof JSArray){
35 | return str.slice(1)
36 | }
37 | if(str instanceof String){
38 | return str.substring(1)
39 | }
40 | throw new RuntimeException("No Such Function: slice")
41 | }
42 |
43 | static def length(str){
44 | if(str.getClass().isArray())return str.length
45 | if(str instanceof List) return str.size()
46 | if(str instanceof String) return str.length()
47 | if(str instanceof JSArray) return str.size()
48 | if(str instanceof Map) return str.size()
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/Main.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler;
2 |
3 |
4 | import cn.wenyan.compiler.command.CommandHandler;
5 | import cn.wenyan.compiler.script.libs.Language;
6 | import cn.wenyan.compiler.utils.ScalaUtils;
7 | import groovy.lang.GroovyClassLoader;
8 |
9 | import java.io.File;
10 |
11 | /**
12 | * 万事开头必有难,指令重器蔑则空。
13 | * 以此以动编译者,指令皆可为君用。
14 | * 亦可帮助亦可行,亦可脚本亦可成。
15 | * 程式终创一场空,来也来去皆也净。
16 | *
17 | * 诚心默念爪哇好,不忘恩赐来世重。
18 | *
19 | */
20 |
21 | public class Main {
22 |
23 | public static void main(String[] args) {
24 | if (args.length == 1 && args[0].equals("shell")) {
25 | WenYanShell.run();
26 | return;
27 | }
28 | if (args.length == 2 && args[0].equals("run")) {
29 | try {
30 | Class> runner = new GroovyClassLoader(Main.class.getClassLoader()).parseClass(new File(args[1]));
31 | runner.getDeclaredMethod("main", String[].class).invoke(null, (Object) args);
32 | } catch (Exception e) {
33 | e.printStackTrace();
34 | return;
35 | }
36 | }
37 |
38 | if (args.length == 0 || args[0].equals("help")) {
39 | CommandHandler.compileCommand.entrySet().stream().forEach(x -> System.out.println(x.getValue().getOption() + ": " + x.getValue().help()));
40 | return;
41 | }
42 |
43 | WenYanCompilerImpl compiler = new WenYanCompilerImpl(false, Language.GROOVY);
44 | long time = ScalaUtils.countTime(() -> {
45 | compiler.compile(args);
46 | return null;
47 | });
48 | compiler.getServerLogger().debug("Use: " + time + "ms");
49 |
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/utils/VarLabel.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.utils;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 | import java.util.Set;
6 |
7 | public class VarLabel {
8 |
9 |
10 | private String name;
11 |
12 | private Map alis;
13 |
14 | private Map lastName;
15 |
16 | public VarLabel() {
17 | this.alis = new HashMap<>();
18 | this.lastName = new HashMap<>();
19 | }
20 |
21 | public String getName() {
22 | return name;
23 | }
24 |
25 | public void setName(String name) {
26 | this.name = name;
27 | }
28 |
29 |
30 | public void addAlis(String funcName,int index,String ali) {
31 | alis.put(funcName+"@"+index,ali);
32 | }
33 |
34 | public String getAlis(String funcName,int ind,boolean defineArg){
35 | String index = funcName+"@"+ind;
36 | if(defineArg){
37 | if(!alis.containsKey(index)){
38 | String n = name+"__"+ind+"__"+funcName;
39 | alis.put(index,n);
40 | lastName.put(index,n);
41 | }
42 | }else{
43 | if(!alis.containsKey(index)){
44 | String ln = lastName.get(index);
45 | alis.put(index,ln==null?name:ln);
46 | lastName.put(index,name);
47 | }
48 | }
49 | return alis.get(index);
50 | }
51 |
52 | private String getIndex(int ind){
53 | ind = ind-1;
54 | Set set = alis.keySet();
55 | for(String s : set){
56 | if(ind == Integer.parseInt(s.split("@")[1])){
57 | return s;
58 | }
59 | }
60 | return null;
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/factory/CompileFactory.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.factory;
2 |
3 | import cn.wenyan.compiler.CompileResult;
4 | import cn.wenyan.compiler.WenYanCompilerImpl;
5 | import cn.wenyan.compiler.streams.CompileStream;
6 | import cn.wenyan.compiler.utils.Assert;
7 |
8 | import java.util.ArrayList;
9 | import java.util.List;
10 |
11 | public class CompileFactory {
12 |
13 | private List streamList;
14 |
15 | private WenYanCompilerImpl compiler;
16 |
17 | private List copy;
18 |
19 | public CompileFactory(List streams,WenYanCompilerImpl compiler){
20 | this.compiler = compiler;
21 | this.streamList = streams;
22 | }
23 |
24 | public void addStream(List stream,Class after){
25 | for(int i = 0;i compile(int index,List wenyan){
34 | copy = new ArrayList<>();
35 | copy.addAll(wenyan);
36 | for(CompileStream stream : streamList){
37 | compiler.callListenerStart(stream,wenyan);
38 | CompileResult result = stream.compile(wenyan);
39 | if(result.isSuccess()){
40 | compiler.callListenerEnd(stream,result.getResult());
41 | return result.getResult();
42 | }else{
43 | if(!wenyan.equals(copy)){
44 | wenyan.clear();
45 | wenyan.addAll(copy);
46 | compiler.clearIndexCode();
47 | }
48 | compiler.callListenerFailed(stream,wenyan);
49 | }
50 | }
51 | return Assert.syntaxError(index+"语句组 Line: "+(compiler.getIndexCode())+": "+wenyan.get(0));
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/streams/ObjectCompileStream.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.streams;
2 |
3 | import cn.wenyan.compiler.CompileResult;
4 | import cn.wenyan.compiler.WenYanCompilerImpl;
5 | import cn.wenyan.compiler.WenYanLib;
6 | import cn.wenyan.compiler.script.libs.LanguageUtils;
7 | import cn.wenyan.compiler.script.libs.Syntax;
8 | import cn.wenyan.compiler.utils.Utils;
9 |
10 | import java.util.List;
11 |
12 | public class ObjectCompileStream extends CompileStream{
13 |
14 | public ObjectCompileStream(WenYanCompilerImpl compiler) {
15 | super(compiler);
16 | }
17 |
18 | @Override
19 | public CompileResult compile(List wenyan) {
20 | VariableCompileStream stream = compiler.getStream(VariableCompileStream.class);
21 | if(Utils.matches(wenyan, WenYanLib.OBJECT_IT())){
22 | compiler.removeWenyan();
23 | StringBuilder builder = new StringBuilder();
24 | String arrName = stream.getNowName();
25 | while (true){
26 | if(Utils.matches(wenyan,WenYanLib.GIVE_OBJECT())) {
27 | String nameDefine = compiler.removeWenyan();
28 | String valueDefine;
29 | if(Utils.matches(wenyan,WenYanLib.GIVE_OBJECT_VALUE())){
30 | valueDefine = compiler.removeWenyan();
31 | }else {
32 | valueDefine = language.getSyntax(Syntax.NULL);
33 |
34 | }
35 |
36 | String name = Utils.getValue(Utils.getString(WenYanLib.STRING(), nameDefine), stream,true);
37 | String value = Utils.getValue(valueDefine.substring(valueDefine.indexOf("曰") + 1),stream);
38 | builder.append(LanguageUtils.arraySet(language, arrName, name, value)).append("\n");
39 | }else{
40 | if(Utils.matches(wenyan,WenYanLib.OBJECT_END())){
41 | compiler.removeWenyan();
42 | }
43 | break;
44 | }
45 | }
46 | return new CompileResult(builder.toString());
47 | }
48 | return new CompileResult(false,wenyan);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/.idea/wenyan-jvm.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/utils/NumberTree.groovy:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.utils
2 |
3 | import groovy.transform.CompileStatic
4 |
5 | import java.util.stream.Collectors;
6 |
7 | @CompileStatic
8 | class NumberTree {
9 |
10 | private int prefix = 1
11 |
12 | private Node root = new Node()
13 |
14 | private BigDecimal floatValue = 0
15 |
16 | private List nodes = new ArrayList<>()
17 |
18 | void inputNumber(String wenyan,Node node){
19 | node.value = wenyan
20 | int index = GroovyUtils.getMax(wenyan)
21 | nodes.remove(node.parent)
22 | nodes.add(node)
23 | if(wenyan.size() == 1)return
24 | if(index == -1)return
25 | String left = wenyan.substring(0,index)
26 | left = left.empty?"一":left
27 | String right = wenyan.substring(index+1)
28 | String max = wenyan.charAt(index)
29 | node.left = new Node()
30 | node.right = new Node()
31 | node.middle = new Node()
32 | node.left.value = left
33 | node.left.parent = node
34 | node.middle.value = max
35 | node.middle.parent = node
36 | node.right.value = right
37 | node.right.parent = node
38 | inputNumber(left,node.left)
39 | inputNumber(max,node.middle)
40 | inputNumber(right,node.right)
41 | }
42 |
43 | NumberTree inputNumber(String wenyan){
44 | if(wenyan.startsWith("負")){
45 | prefix = -prefix
46 | wenyan = wenyan.substring(1)
47 | }
48 | if(wenyan.contains("又")){
49 | def numbers = wenyan.split("又")
50 | inputNumber(numbers[0],root)
51 | floatValue = GroovyUtils.getNumber(numbers[1])
52 | }else{
53 | inputNumber(wenyan,root)
54 | }
55 | this
56 | }
57 |
58 | BigDecimal convertToNumber(){
59 | BigDecimal result = 0
60 | for(int i = 0;i compileToClass(String className,String... wenyanString);
52 |
53 | void runFile(String file,String[] args);
54 |
55 | void runFile(String file);
56 |
57 | void runFile(File file) throws IOException;
58 |
59 | // = compile(wenyan)
60 | String dispatch(String wenyan);
61 |
62 | // 简体中文 to 繁体中文
63 | String getTraditionalChinese(String wenyan);
64 | }
65 |
66 | ```
67 |
68 | #### Current progress
69 |
70 | 1. Currently in development, not all syntax is complete
71 | 2. The syntax of the implementation can be found in [README](https://github.com/MagicLu550/wenyan-lang_jvm/blob/master/README.md)
72 |
73 |
--------------------------------------------------------------------------------
/project_example/src/main/java/藏書樓/子曰/序.wy:
--------------------------------------------------------------------------------
1 | 今有一術。名之曰「子曰」。欲行是術。必先得一言。曰「言」。乃行是術曰。
2 |
3 | 吾有一言。曰「「 __._ 」」。 書之。
4 | 吾有一言。曰「「 / ___)_ 」」。 書之。
5 | 吾有一言。曰「「 (_/Y ===\\ __ 」」。 書之。
6 | 吾有一言。曰「「 |||.==. =). | 」」。 書之。
7 | 吾有一言。曰「「 |((| o |p| | 」」。加其以「言」。書之。
8 | 吾有一言。曰「「 _./| \\( /=\\ ) |__ 」」。 書之。
9 | 吾有一言。曰「「 / |@\\ ||||||||. 」」。 書之。
10 | 吾有一言。曰「「 / \\@\\ ||||||||\\ 」」。 書之。
11 | 吾有一言。曰「「 / \\ \\@\\ ||||||//\\ 」」。 書之。
12 | 吾有一言。曰「「 ( Y \\@\\|||| // _\\ 」」。 書之。
13 | 吾有一言。曰「「 | -\\ \\@\\ \\\\// \\ 」」。 書之。
14 | 吾有一言。曰「「 | -\\__.-./ //\\.---.^__ 」」。 書之。
15 | 吾有一言。曰「「 | \\ / |@|__/\\_|@| | | 」」。 書之。
16 | 吾有一言。曰「「 \\__\\ |@||| |||@| | 」」。 書之。
17 | 吾有一言。曰「「 <@@@| |@||| |||@| / 」」。 書之。
18 | 吾有一言。曰「「 / ---| /@||| |||@| / 」」。 書之。
19 | 吾有一言。曰「「 | /| /@/ || |||@| /| 」」。 書之。
20 | 吾有一言。曰「「 | //| /@/ ||_|||@| / | 」」。 書之。
21 | 吾有一言。曰「「 | // \\ ||@| /|=|||@| | | 」」。 書之。
22 | 吾有一言。曰「「 \\ // \\||@| / |/|||@| \\ | 」」。 書之。
23 | 吾有一言。曰「「 |// ||@| / ,/|||@| | 」」。 書之。
24 | 吾有一言。曰「「 // ||@|/ /|/||/@/ | 」」。 書之。
25 | 吾有一言。曰「「 //| , ||// /\\|/\\/@/ / / 」」。 書之。
26 | 吾有一言。曰「「 //\\ / \\|/ /H\\|/H\\/ /_/ 」」。 書之。
27 | 吾有一言。曰「「 // |\\_/ |__/|H\\|/H|\\_/ 」」。 書之。
28 | 吾有一言。曰「「 |/ |\\ / |H===H| | 」」。 書之。
29 | 吾有一言。曰「「 ||\\ /| |H|||H| | 」」。 書之。
30 | 吾有一言。曰「「 ||______/ | |H|||H| | 」」。 書之。
31 | 吾有一言。曰「「 \\_/ _/ _/ |L|||J| \\_ 」」。 書之。
32 | 吾有一言。曰「「 _/ ___/ ___\\__/___ '-._ 」」。 書之。
33 | 吾有一言。曰「「 /__________/===\\__/===\\---' 」」。 書之。
34 | 吾有一言。曰「「 」」。 書之。
35 |
36 | 是謂「子曰」之術也。
37 |
38 | 或云「「子曰「甲」」」。
39 | 蓋謂「「施「子曰」於「甲」」」。
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/lang/列經.wy:
--------------------------------------------------------------------------------
1 | 疏曰『
2 |
3 | 此經乃改編者以順java之道
4 |
5 | 參見 https://github.com/wenyan-lang/wenyan/tree/master/lib
6 |
7 |
8 | 』
9 | 或云「「凡「甲」皆「乙」其上者」」。
10 | 蓋謂「「施「遍施」於「乙」於「甲」」」
11 |
12 | 或云「「唯「甲」中得「乙」者」」
13 | 蓋謂「「施「篩剔」於「乙」於「甲」」」
14 |
15 | 或云「「併「甲」於「乙」以「丙」者」」
16 | 蓋謂「「施「左併」於「丙」於「乙」於「甲」」」
17 |
18 | 注曰「「遍施。同Javascript之Array.forEach也。」」
19 | 今有一術。名之曰「遍施」。欲行是術。必先得一術。曰「甲」。一列。曰「乙」。乃行是術曰。
20 | 吾有一列。名之曰「丙」。
21 | 凡「乙」中之「丁」。
22 | 施「甲」於「丁」。充「丙」以其也。
23 | 乃得「丙」。
24 | 是謂「遍施」之術也。
25 |
26 | 注曰「「篩剔。同Javascript之Array.filter也。」」
27 | 今有一術。名之曰「篩剔」。欲行是術。必先得一術。曰「甲」。一列。曰「乙」。乃行是術曰。
28 | 吾有一列。名之曰「丙」。
29 | 凡「乙」中之「丁」。
30 | 施「甲」於「丁」。
31 | 若其者。充「丙」以「丁」也。
32 | 云云。
33 | 乃得「丙」。
34 | 是謂「篩剔」之術也。
35 |
36 | 今有一術。名之曰「左併」。欲行是術。必先得一術。曰「甲」。一元。曰「乙」。一列。曰「丙」。乃行是術曰。
37 | 吾有一元曰「乙」。名之曰「丁」。
38 | 凡「丙」中之「戊」。
39 | 施「甲」於「丁」於「戊」。
40 | 昔之「丁」者。今其是矣。
41 | 云云。
42 | 乃得「丁」。
43 | 是謂「左併」之術也。
44 |
45 |
46 | 今有一術。名之曰「右併」。欲行是術。必先得一術。曰「甲」。一元。曰「乙」。一列。曰「丙」。乃行是術曰。
47 | 吾有一元曰「乙」。名之曰「丁」。
48 | 夫「丙」之長。名之曰「戊」。
49 | 恆為是。若「戊」小於一者乃止也。
50 | 夫「丙」之「戊」。夫「丁」。取二以施「甲」。
51 | 昔之「丁」者。今其是矣。
52 | 減「戊」以一。昔之「戊」者。今其是矣。云云。
53 | 乃得「丁」。
54 | 是謂「右併」之術也。
55 |
56 | 注曰「「排序。同Javascript之Array.sort也。」」
57 | 今有一術。名之曰「排序」。欲行是術。必先得一術。曰「甲」。一列。曰「乙」。乃行是術曰。
58 |
59 | 若「乙」之長不大於一者。乃得「乙」也。
60 | 吾有三列。名之曰「首」。曰「頷」。曰「尾」。
61 |
62 | 夫「乙」之一。名之曰「乙一」。
63 | 充「頷」以「乙一」。
64 | 夫「乙」之其餘。名之曰「乙餘」。
65 |
66 | 凡「乙餘」中之「丁」。
67 | 施「甲」於「丁」於「乙一」。
68 | 若其小於零者。
69 | 充「首」以「丁」。
70 | 若非。
71 | 充「尾」以「丁」
72 | 也。
73 | 云云。
74 | 施「排序」於「甲」於「首」。昔之「首」者。今其是矣。
75 | 施「排序」於「甲」於「尾」。昔之「尾」者。今其是矣。
76 |
77 | 銜「首」以「頷」以「尾」。名之曰「丙」。
78 | 乃得「丙」。
79 |
80 | 是謂「排序」之術也。
81 |
82 | 注曰「「倒序。同Javascript之Array.reverse也。」」
83 | 今有一術。名之曰「倒序」。欲行是術。必先得一列。曰「甲」。乃行是術曰。
84 | 吾有一列。名之曰「乙」。
85 | 夫「甲」之長。名之曰「戊」。
86 | 恆為是。若「戊」小於一者乃止也。
87 | 夫「甲」之「戊」。充「乙」以其。
88 | 減「戊」以一。昔之「戊」者。今其是矣。云云。
89 | 乃得「乙」。
90 | 是謂「倒序」之術也。
91 |
92 | 注曰「「擷取。同Javascript之Array.slice也。」」
93 | 今有一術。名之曰「擷取」。欲行是術。必先得一列。曰「甲」。二數。曰「乙」曰「丙」。乃行是術曰。
94 | 吾有一列。名之曰「丁」。
95 | 吾有一數。曰「乙」。名之曰「戊」。
96 | 若「丙」小於零者。夫「甲」之長。加其以「丙」。昔之「丙」者。今其是矣。云云。
97 |
98 | 恆為是。若「戊」大於「丙」者乃止也。
99 | 夫「甲」之「戊」。充「丁」以其。
100 | 加「戊」以一。昔之「戊」者。今其是矣。云云。
101 | 乃得「丙」。
102 |
103 | 是謂「擷取」之術也。
104 |
105 |
106 | 今有一術。名之曰「製列」。欲行是術。必先得一數。曰「甲」。一術。曰「乙」。一列。曰「丙」。乃行是術曰。
107 | 吾有一列。名之曰「丁」。
108 | 吾有一數曰一。名之曰「戊」。
109 |
110 | 恆為是。若「戊」大於「甲」者乃止也。
111 | 施「乙」於「戊」。充「丁」以其。
112 | 加「戊」以一。昔之「戊」者。今其是矣。云云。
113 | 乃得「丁」。
114 | 是謂「製列」之術也。
115 |
116 |
117 | 今有一術。名之曰「索一」。欲行是術。必先得一列。曰「甲」。一元。曰「乙」。乃行是術曰。
118 | 吾有一數曰一。名之曰「戊」。
119 | 恆為是。若「戊」大於「甲」之長者乃止也。
120 | 若「乙」等於「甲」之「戊」者。乃得「戊」也。
121 | 加「戊」以一。昔之「戊」者。今其是矣。云云。
122 | 乃得負一。
123 | 是謂「索一」之術也。
124 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/command/CommandHandler.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.command;
2 |
3 | import cn.wenyan.compiler.WenYanCompilerImpl;
4 | import cn.wenyan.compiler.exceptions.CommandException;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 | import java.util.Map;
9 | import java.util.concurrent.ConcurrentHashMap;
10 |
11 | public class CommandHandler {
12 |
13 | public static Map compileCommand = new ConcurrentHashMap<>();
14 |
15 | private WenYanCompilerImpl compiler;
16 |
17 | static {
18 | registerCommands();
19 | }
20 |
21 | public static void registerCommands(){
22 | compileCommand.put("-p",new PinYinCommand());
23 | compileCommand.put("-c",new CompileFileCommand());
24 | compileCommand.put("-o",new OutFileCommand());
25 | compileCommand.put("-l",new CompileLibCommand());
26 | compileCommand.put("-r",new RunCommand());
27 | compileCommand.put("-sc",new SourceCommand());
28 | compileCommand.put("-m",new MainClassCommand());
29 | compileCommand.put("-g",new GroovyCommand());
30 | compileCommand.put("-n",new ClassFileCommand());
31 | compileCommand.put("-wyg",new WenYuanGeCommand());
32 | compileCommand.put("-wd",new WenYuanGeDownloadCommand());
33 | compileCommand.put("-lv",new LexerViewerCommand());
34 | compileCommand.put("-st",new StrongTypeCommand());
35 | }
36 |
37 | public CommandHandler(WenYanCompilerImpl compiler){
38 | this.compiler =compiler;
39 | }
40 |
41 | public int executeCommand(String[] args){
42 | CompilerConfig compilerConfig = new CompilerConfig();
43 | for(int index = 0;index arr = new ArrayList<>();
45 | Command command = compileCommand.get(args[index]);
46 | if(command!=null){
47 | int len = command.getArgsLength();
48 | if(len == -1){
49 | for(int j = 0;index getCompileCommand() {
70 | return compileCommand;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/plugins/PluginManager.java:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.plugins;
2 |
3 | import cn.wenyan.compiler.WenYanCompilerImpl;
4 | import cn.wenyan.compiler.WenYanLib;
5 | import cn.wenyan.compiler.streams.CompileStream;
6 | import cn.wenyan.compiler.utils.Utils;
7 |
8 | import java.io.File;
9 | import java.io.IOException;
10 | import java.util.*;
11 | import java.util.jar.JarEntry;
12 | import java.util.jar.JarFile;
13 |
14 | public class PluginManager {
15 |
16 | private Map plugins = new HashMap<>();
17 |
18 | private WenYanCompilerImpl compiler;
19 |
20 | public PluginManager(WenYanCompilerImpl impl){
21 | this.compiler = impl;
22 | }
23 |
24 | public Plugin loadPlugin(File file){
25 | try {
26 | JavaPluginClassloader cl = new JavaPluginClassloader(file);
27 | JarFile jar = new JarFile(file);
28 | Main main = null;
29 | Enumeration entries = jar.entries();
30 | while (entries.hasMoreElements()){
31 | JarEntry entry = entries.nextElement();
32 | String name = Utils.getClassName(entry.getName());
33 | Class clz = cl.loadClass(name);
34 | main = (Main) clz.getAnnotation(Main.class);
35 | if(main != null){
36 | break;
37 | }
38 | }
39 | if(main == null){
40 | throw new ClassNotFoundException("No main class");
41 | }else{
42 | String name = main.name();
43 | Class> clz = main.after();
44 | try {
45 | Plugin plugin = (Plugin) clz.newInstance();
46 | List streamList = new ArrayList<>();
47 | List listeners = new ArrayList<>();
48 | plugin.addCompileStream(streamList);
49 | plugin.addSyntaxRegex(WenYanLib.syntaxs());
50 | plugin.addListener(listeners);
51 | plugin.addPatterns(WenYanLib.patterns());
52 | compiler.getFactory().addStream(streamList,clz);
53 | compiler.getListeners().addAll(listeners);
54 | plugins.put(name,plugin);
55 | compiler.getServerLogger().info("加载插件: "+name+" 成功");
56 | return plugin;
57 | }catch (Exception e){
58 | compiler.getServerLogger().error("",e);
59 | }
60 |
61 | }
62 | }catch (ClassNotFoundException|IOException e){
63 | compiler.getServerLogger().error("",e);
64 | }
65 | return null;
66 | }
67 |
68 | public Map getPlugins() {
69 | return plugins;
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/log/LogFormat.java:
--------------------------------------------------------------------------------
1 | /*
2 | Jsmod2 is a java-based scpsl cn.jsmod2.server initiated by jsmod2.cn.
3 | It needs to rely on smod2 and proxy. jsmod2 is an open source
4 | free plugin that is released under the GNU license. Please read
5 | the GNU open source license before using the software. To understand
6 | the appropriateness, if infringement, will be handled in accordance
7 | with the law, @Copyright Jsmod2 China,more can see that
8 | */
9 | package cn.wenyan.compiler.log;
10 |
11 | import org.fusesource.jansi.Ansi;
12 |
13 | import java.time.LocalDateTime;
14 | import java.time.format.DateTimeFormatter;
15 | import java.util.HashMap;
16 | import java.util.Map;
17 |
18 | import static org.fusesource.jansi.Ansi.Color.*;
19 |
20 | public class LogFormat {
21 |
22 | private static Map colors;
23 |
24 | static {
25 | colors = new HashMap<>();
26 | colors.put(BLACK,"\033[30m");
27 | colors.put(RED,"\033[31m");
28 | colors.put(GREEN,"\033[32m");
29 | colors.put(YELLOW,"\033[33m");
30 | colors.put(BLUE,"\033[34m");
31 | colors.put(MAGENTA,"\033[35m");
32 | colors.put(CYAN,"\033[36m");
33 | colors.put(WHITE,"\033[37m");
34 | colors.put(DEFAULT,"\033[0m");
35 | }
36 |
37 | public static String fg(Ansi.Color c){
38 | return colors.get(c);
39 | }
40 |
41 | private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
42 |
43 | public static String format(String message, String type, Ansi.Color color,String prefix){
44 | return format(message,type,color,prefix,false);
45 | }
46 |
47 | public static String format(String message, String type, Ansi.Color color,String prefix,boolean controlMain){
48 | return prefix+fg(MAGENTA)+dateTimeFormatter.format(LocalDateTime.now())+fg(DEFAULT)+"["+fg(color)+(controlMain?control(type, Control.GLISTEN):type)+fg(DEFAULT)+"\t]"+fg(DEFAULT)+" "+message;
49 | //return ansi().eraseScreen().a(prefix).fg(MAGENTA).a(dateTimeFormatter.format(LocalDateTime.now())).fg(DEFAULT).a("[").fg(color).a(type).fg(DEFAULT).a("\t]").fg(BLUE).a(" "+message).reset();
50 | }
51 |
52 | public static String textFormat(String message, Ansi.Color color){
53 | return fg(color)+message;
54 | //return ansi().eraseScreen().fg(color).a(message).reset();
55 | }
56 |
57 | public static String control(String message,Control c){
58 | return c.getAnsi()+message;
59 | }
60 |
61 | public enum Control{
62 |
63 | GLISTEN("\033[5m"),
64 |
65 | BOLD("\033[1m");
66 |
67 | private String ansi;
68 | Control(String ansi){
69 | this.ansi = ansi;
70 | }
71 |
72 | public String getAnsi() {
73 | return ansi;
74 | }
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/src/main/java/cn/wenyan/compiler/lib/JSArray.groovy:
--------------------------------------------------------------------------------
1 | package cn.wenyan.compiler.lib
2 |
3 | import groovy.transform.CompileStatic
4 | import groovy.transform.TypeCheckingMode
5 |
6 | import java.util.Map.Entry
7 |
8 | @CompileStatic
9 | class JSArray extends HashMap