mirror of
https://github.com/HigherOrderCO/Kind1.git
synced 2024-09-11 08:45:32 +03:00
Stringify string literals on Rust side
This commit is contained in:
parent
b109ee674f
commit
60cf29dfeb
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -176,7 +176,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "kind2"
|
||||
version = "0.2.59"
|
||||
version = "0.2.62"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"highlight_error",
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "kind2"
|
||||
version = "0.2.62"
|
||||
version = "0.2.63"
|
||||
edition = "2021"
|
||||
description = "A pure functional functional language that uses the HVM."
|
||||
repository = "https://github.com/Kindelia/Kind2"
|
||||
|
@ -1885,7 +1885,30 @@ pub fn compile_book(book: &Book) -> String {
|
||||
// Stringification
|
||||
// ===============
|
||||
|
||||
pub fn interpret_as_string(term: &Term) -> Option<String> {
|
||||
let mut text = String::new();
|
||||
let mut term = term;
|
||||
loop {
|
||||
if let Term::Ctr { name, args, .. } = term {
|
||||
if name == "String.cons" && args.len() == 2 {
|
||||
if let Term::Num { numb, .. } = *args[0] {
|
||||
text.push(char::from_u32(numb as u32).unwrap_or('\0'));
|
||||
term = &*args[1];
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if name == "String.nil" && args.len() == 0 {
|
||||
return Some(text);
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show_term(term: &Term) -> String {
|
||||
if let Some(as_string) = interpret_as_string(term) {
|
||||
return format!("\"{}\"", as_string);
|
||||
}
|
||||
match term {
|
||||
Term::Typ { .. } => {
|
||||
format!("Type")
|
||||
|
@ -4,6 +4,9 @@
|
||||
use crate::language::{*};
|
||||
|
||||
pub fn to_hvm_term(book: &Book, term: &Term) -> String {
|
||||
if let Some(as_string) = interpret_as_string(term) {
|
||||
return format!("\"{}\"", as_string);
|
||||
}
|
||||
match term {
|
||||
Term::Typ { .. } => {
|
||||
format!("Type")
|
||||
|
Loading…
Reference in New Issue
Block a user