Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(kclvm-runner): encapsulate dylib generating, linking and exe…
…cuting in kclvm/lib.rs into kclvm-runner Encapsulate dylibs generating and executing in kclvm/lib.rs into kclvm-runner. Add struct "KclvmAssembler" in kclvm-runner/runner.rs to provide method "gen_dylibs" for dylibs generating. Add struct "KclvmLinker" in kclvm-runner/runner.rs to provide method "link_all_dylibs" for dylib linking. Add method "execute" in kclvm-runner/lib.rs to encapsulate dylibs generating(gen_dylib), dylib linking(link_all_dylib) and running(runner.run) together The main purpose of separating the three parts from kclvm/lib.rs and encapsulating them into kclvm-runner is to support reuse in kcl-vet. fix #67
- Loading branch information
zong-zhe
committed
Jun 2, 2022
1 parent
c562c0a
commit 4e0c496
Showing
16 changed files
with
399 additions
and
147 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,42 @@ | ||
use kclvm_ast::ast::Program; | ||
use kclvm_sema::resolver::scope::ProgramScope; | ||
use runner::{ExecProgramArgs, KclvmAssembler, KclvmLinker, KclvmRunner, KclvmRunnerOptions}; | ||
|
||
pub mod command; | ||
pub mod runner; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
/// Returns the KCL program executing result as Result< a_json_string, an_err_string >. | ||
/// program: ast.Program returned by kclvm-parser. | ||
/// scope: ProgramScope returned by kclvm-sema | ||
/// | ||
/// In the method, dylibs is generated by KclvmAssembler, and method "KclvmAssembler::gen_dylibs" | ||
/// will return dylibs path in a "Vec<String>"; | ||
/// | ||
/// After linking all dylibs by KclvmLinker, method "KclvmLinker::link_all_dylibs" will return a path | ||
/// for dylib. | ||
/// | ||
/// At last, KclvmRunner will be constructed and call method "run" to execute the kcl program. | ||
pub fn execute( | ||
program: Program, | ||
scope: ProgramScope, | ||
plugin_agent: u64, | ||
args: &ExecProgramArgs, | ||
) -> Result<String, String> { | ||
// generate dylibs | ||
let dylib_paths = KclvmAssembler::gen_dylibs(program, scope, plugin_agent); | ||
|
||
// link dylibs | ||
let dylib_path = KclvmLinker::link_all_dylibs(dylib_paths, plugin_agent); | ||
|
||
// run | ||
let runner = KclvmRunner::new( | ||
dylib_path.as_str(), | ||
Some(KclvmRunnerOptions { | ||
plugin_agent_ptr: plugin_agent, | ||
}), | ||
); | ||
runner.run(&args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
schema Person: | ||
name: str | ||
age: int | ||
gender: str | ||
info: str = "{}, {}, {} years old".format(name, gender, age) | ||
|
||
alice = Person { | ||
"name": "alice", | ||
"age": 10, | ||
"gender": "female" | ||
} |
1 change: 1 addition & 0 deletions
1
kclvm/runner/src/test_datas/init_check_order_0/stdout.golden.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"alice": {"name": "alice", "age": 10, "gender": "female", "info": "alice, female, 10 years old", "__settings__": {"output_type": "INLINE", "__schema_type__": "__main__.Person"}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
schema Name: | ||
mixin [UpperMixin] | ||
firstName: str | ||
lastName: str | ||
upper: str | ||
|
||
# print("init name") | ||
|
||
schema Person(Name): | ||
gender: str | ||
title: str | ||
info: str | ||
|
||
# print("init person") | ||
|
||
schema Girl(Person): | ||
mixin [TitleMixin, InfoMixin] | ||
gender: str = "female" | ||
added: str = "some girl attr" | ||
|
||
# print("init girl") | ||
|
||
check: | ||
gender == "female", "gender should be female in Girl" | ||
|
||
schema Boy(Person): | ||
mixin [TitleMixin, InfoMixin] | ||
gender: str = "male" | ||
added: str = "some boy attr" | ||
|
||
# print("init boy") | ||
|
||
check: | ||
gender == "male", "gender should be male in Boy" | ||
|
||
schema UpperMixin: | ||
# print("init upperMixin") | ||
upper: str = lastName.upper() | ||
|
||
schema TitleMixin: | ||
# print("init title mixin") | ||
if gender == "female": | ||
title = "Ms.{}".format(lastName) | ||
else: | ||
title = "Mr.{}".format(lastName) | ||
|
||
schema InfoMixin: | ||
# print("init info mixin") | ||
info = "{}, {}".format(title, gender) | ||
|
||
alice = Girl { | ||
"firstName": "Alice", | ||
"lastName": "Smith" | ||
} | ||
|
||
# print(" ===") | ||
|
||
bob = Boy { | ||
"firstName": "Bob", | ||
"lastName": "Green" | ||
} |
1 change: 1 addition & 0 deletions
1
kclvm/runner/src/test_datas/init_check_order_1/stdout.golden.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"alice": {"firstName": "Alice", "lastName": "Smith", "upper": "SMITH", "__settings__": {"output_type": "INLINE", "__schema_type__": "__main__.InfoMixin"}, "gender": "female", "title": "Ms.Smith", "info": "Ms.Smith, female", "added": "some girl attr"}, "bob": {"firstName": "Bob", "lastName": "Green", "upper": "GREEN", "__settings__": {"output_type": "INLINE", "__schema_type__": "__main__.InfoMixin"}, "gender": "male", "title": "Mr.Green", "info": "Mr.Green, male", "added": "some boy attr"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
schema NumberMap: | ||
[num: str]: int | ||
|
||
check: | ||
int(num) % 2 == 0 | ||
|
||
numMap = NumberMap { | ||
str(0): 0 | ||
str(2): 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"numMap":{"0":0,"2":2,"__settings__":{"__schema_type__":"__main__.NumberMap","output_type":"INLINE"}}} |
6 changes: 6 additions & 0 deletions
6
kclvm/runner/src/test_datas/type_annotation_not_full_2/main.k
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
schema A[arg=1]: | ||
a: int = arg | ||
|
||
a1 = A() | ||
a2 = A(2) | ||
a3 = A(arg=3) |
1 change: 1 addition & 0 deletions
1
kclvm/runner/src/test_datas/type_annotation_not_full_2/stdout.golden.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"a1":{"__settings__":{"__schema_type__":"__main__.A","output_type":"INLINE"},"a":1},"a2":{"__settings__":{"__schema_type__":"__main__.A","output_type":"INLINE"},"a":2},"a3":{"__settings__":{"__schema_type__":"__main__.A","output_type":"INLINE"},"a":3}} |
Oops, something went wrong.