Fix used builtin logic

This commit is contained in:
imaqtkatt 2024-01-26 17:26:58 -03:00
parent 1e3465235c
commit 114e971352
4 changed files with 44 additions and 3 deletions

View File

@ -78,6 +78,7 @@ impl Term {
impl Pattern {
pub fn encode_lists(&mut self) -> bool {
match self {
Pattern::Var(Some(Name(nam))) => nam == LNIL,
Pattern::List(pats) => {
let lnil = Pattern::Var(Some(Name::new(LNIL)));
@ -88,8 +89,8 @@ impl Pattern {
true
}
Pattern::Ctr(_, pats) => {
let mut uses = false;
Pattern::Ctr(Name(nam), pats) => {
let mut uses = nam == LCONS;
for pat in pats {
uses |= pat.encode_lists();
}

View File

@ -1,7 +1,7 @@
use hvmc::run::Val;
use indexmap::IndexMap;
use crate::term::{builtin_adt, Book, Name, Term};
use crate::term::{builtin_adt, Book, Name, Pattern, Term};
pub const STRING: &str = "String";
pub const SNIL: &str = "SNil";
@ -16,6 +16,7 @@ impl builtin_adt::BuiltinAdt for BuiltinString {
let mut found_str = false;
for def in book.defs.values_mut() {
for rule in &mut def.rules {
rule.pats.iter_mut().for_each(|pat| found_str |= pat.encode_str());
found_str |= rule.body.encode_str();
}
}
@ -75,3 +76,19 @@ impl Term {
}
}
}
impl Pattern {
pub fn encode_str(&mut self) -> bool {
match self {
Pattern::Var(Some(Name(nam))) => nam == SNIL,
Pattern::Ctr(Name(nam), pats) => {
let mut uses = nam == SCONS;
for pat in pats {
uses |= pat.encode_str();
}
uses
}
_ => false,
}
}
}

View File

@ -0,0 +1,4 @@
main = *
Foo SNil = 0
Foo * = 1

View File

@ -0,0 +1,19 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/encode_pattern_match/no_literal_builtin.hvm
---
(main) = *
(Foo) = λx #String (x Foo$PSCons Foo$PSNil)
(SCons) = λhead λtail #String λSCons #String λSNil #String.SCons.tail (#String.SCons.head (SCons head) tail)
(SNil) = #String λSCons #String λSNil SNil
(Foo$R0) = 0
(Foo$R1) = λ* 1
(Foo$PSCons) = #String.SCons.head λy0 #String.SCons.tail λy1 (Foo$R1 *)
(Foo$PSNil) = Foo$R0