-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce built-in contexts in language definition
- Loading branch information
Showing
28 changed files
with
539 additions
and
470 deletions.
There are no files selected for viewing
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
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
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,55 +1,56 @@ | ||
use std::fmt::{Error, Write}; | ||
|
||
use codegen_language_definition::model::BuiltIn; | ||
use codegen_language_definition::model::{BuiltIn, BuiltInContext}; | ||
|
||
pub fn render_built_ins(built_ins: &[BuiltIn]) -> String { | ||
try_render_built_ins(built_ins).expect("Failed to render built-ins") | ||
pub fn render_built_ins(built_in_contexts: &[BuiltInContext]) -> String { | ||
try_render_built_ins(built_in_contexts).expect("Failed to render built-ins") | ||
} | ||
|
||
fn try_render_built_ins(built_ins: &[BuiltIn]) -> Result<String, Error> { | ||
fn try_render_built_ins(built_in_contexts: &[BuiltInContext]) -> Result<String, Error> { | ||
let mut buffer = String::new(); | ||
// __SLANG_SOLIDITY_BUILT_INS_CONTRACT_NAME__ keep in sync with binding rules | ||
writeln!(buffer, "contract $BuiltIns$ {{")?; | ||
for item in built_ins { | ||
match item { | ||
BuiltIn::BuiltInFunction { item } => { | ||
writeln!( | ||
buffer, | ||
" function {name}({parameters}) public{return_type};", | ||
name = item.name, | ||
parameters = item.parameters.join(", "), | ||
return_type = item | ||
.return_type | ||
.as_ref() | ||
.map(|return_type| format!(" returns ({return_type})")) | ||
.unwrap_or_default(), | ||
)?; | ||
} | ||
BuiltIn::BuiltInType { item } => { | ||
writeln!(buffer, " struct {name} {{", name = item.name)?; | ||
for field in &item.fields { | ||
writeln!(buffer, " {field_def};", field_def = field.definition)?; | ||
} | ||
for function in &item.functions { | ||
for context in built_in_contexts { | ||
writeln!(buffer, "contract {context} {{", context = context.name)?; | ||
for item in &context.definitions { | ||
match item { | ||
BuiltIn::BuiltInFunction { item } => { | ||
writeln!( | ||
buffer, | ||
" function({parameters}){return_type} {name};", | ||
name = function.name, | ||
return_type = function | ||
" function {name}({parameters}) public{return_type};", | ||
name = item.name, | ||
parameters = item.parameters.join(", "), | ||
return_type = item | ||
.return_type | ||
.as_deref() | ||
.as_ref() | ||
.map(|return_type| format!(" returns ({return_type})")) | ||
.unwrap_or_default(), | ||
parameters = function.parameters.join(", "), | ||
)?; | ||
} | ||
writeln!(buffer, " }}")?; | ||
} | ||
BuiltIn::BuiltInVariable { item } => { | ||
writeln!(buffer, " {var_def};", var_def = item.definition)?; | ||
BuiltIn::BuiltInType { item } => { | ||
writeln!(buffer, " struct {name} {{", name = item.name)?; | ||
for field in &item.fields { | ||
writeln!(buffer, " {field_def};", field_def = field.definition)?; | ||
} | ||
for function in &item.functions { | ||
writeln!( | ||
buffer, | ||
" function({parameters}){return_type} {name};", | ||
name = function.name, | ||
return_type = function | ||
.return_type | ||
.as_deref() | ||
.map(|return_type| format!(" returns ({return_type})")) | ||
.unwrap_or_default(), | ||
parameters = function.parameters.join(", "), | ||
)?; | ||
} | ||
writeln!(buffer, " }}")?; | ||
} | ||
BuiltIn::BuiltInVariable { item } => { | ||
writeln!(buffer, " {var_def};", var_def = item.definition)?; | ||
} | ||
} | ||
} | ||
writeln!(buffer, "}}")?; | ||
} | ||
writeln!(buffer, "}}")?; | ||
Ok(buffer) | ||
} |
Oops, something went wrong.