From e0e98f92e4ce914ec7bfb21781fae903b72eac74 Mon Sep 17 00:00:00 2001 From: Kasey White Date: Tue, 10 May 2022 23:58:37 -0400 Subject: [PATCH] fix: builtin parsing --- example/plutus-core | 2 +- src/ast.rs | 80 ++------------------------------------------- src/builtins.rs | 78 +++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/parser.rs | 8 +++-- 5 files changed, 87 insertions(+), 82 deletions(-) create mode 100644 src/builtins.rs diff --git a/example/plutus-core b/example/plutus-core index 76f101ea..e91bf68f 100644 --- a/example/plutus-core +++ b/example/plutus-core @@ -1,3 +1,3 @@ (program 1.0.0 - [(lam x (con integer 4)) (con bool False)] + [[(builtin addInteger) (con integer 4)] (con integer 8)] ) \ No newline at end of file diff --git a/src/ast.rs b/src/ast.rs index 672469cc..b18043fe 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1,4 +1,4 @@ -use strum_macros::EnumString; +use crate::builtins::DefaultFunction; #[derive(Debug)] pub struct Program { @@ -27,7 +27,7 @@ pub enum Term { // tag: 5 Force(Box), // tag: 6 - Error(Box), + Error, // tag: 7 Builtin(DefaultFunction), } @@ -48,79 +48,3 @@ pub enum Constant { // tag: 5 Bool(bool), } - -#[allow(non_camel_case_types)] -#[derive(Debug, Clone, EnumString)] -pub enum DefaultFunction { - // Integer functions - AddInteger, - SubtractInteger, - MultiplyInteger, - DivideInteger, - QuotientInteger, - RemainderInteger, - ModInteger, - EqualsInteger, - LessThanInteger, - LessThanEqualsInteger, - // ByteString functions - AppendByteString, - ConsByteString, - SliceByteString, - LengthOfByteString, - IndexByteString, - EqualsByteString, - LessThanByteString, - LessThanEqualsByteString, - // Cryptography and hash functions - Sha2_256, - Sha3_256, - Blake2b_256, - VerifySignature, - VerifyEcdsaSecp256k1Signature, - VerifySchnorrSecp256k1Signature, - // String functions - AppendString, - EqualsString, - EncodeUtf8, - DecodeUtf8, - // Bool function - IfThenElse, - // Unit function - ChooseUnit, - // Tracing function - Trace, - // Pairs functions - FstPair, - SndPair, - // List functions - ChooseList, - MkCons, - HeadList, - TailList, - NullList, - // Data functions - // It is convenient to have a "choosing" function for a data type that has more than two - // constructors to get pattern matching over it and we may end up having multiple such data - // types, hence we include the name of the data type as a suffix. - ChooseData, - ConstrData, - MapData, - ListData, - IData, - BData, - UnConstrData, - UnMapData, - UnListData, - UnIData, - UnBData, - EqualsData, - SerialiseData, - // Misc constructors - // Constructors that we need for constructing e.g. Data. Polymorphic builtin - // constructors are often problematic (See note [Representable built-in - // functions over polymorphic built-in types]) - MkPairData, - MkNilData, - MkNilPairData, -} diff --git a/src/builtins.rs b/src/builtins.rs new file mode 100644 index 00000000..7016701a --- /dev/null +++ b/src/builtins.rs @@ -0,0 +1,78 @@ +use strum_macros::EnumString; + +#[allow(non_camel_case_types)] +#[derive(Debug, Clone, EnumString)] +#[strum(serialize_all = "camelCase")] +pub enum DefaultFunction { + // Integer functions + AddInteger, + SubtractInteger, + MultiplyInteger, + DivideInteger, + QuotientInteger, + RemainderInteger, + ModInteger, + EqualsInteger, + LessThanInteger, + LessThanEqualsInteger, + // ByteString functions + AppendByteString, + ConsByteString, + SliceByteString, + LengthOfByteString, + IndexByteString, + EqualsByteString, + LessThanByteString, + LessThanEqualsByteString, + // Cryptography and hash functions + Sha2_256, + Sha3_256, + Blake2b_256, + VerifySignature, + VerifyEcdsaSecp256k1Signature, + VerifySchnorrSecp256k1Signature, + // String functions + AppendString, + EqualsString, + EncodeUtf8, + DecodeUtf8, + // Bool function + IfThenElse, + // Unit function + ChooseUnit, + // Tracing function + Trace, + // Pairs functions + FstPair, + SndPair, + // List functions + ChooseList, + MkCons, + HeadList, + TailList, + NullList, + // Data functions + // It is convenient to have a "choosing" function for a data type that has more than two + // constructors to get pattern matching over it and we may end up having multiple such data + // types, hence we include the name of the data type as a suffix. + ChooseData, + ConstrData, + MapData, + ListData, + IData, + BData, + UnConstrData, + UnMapData, + UnListData, + UnIData, + UnBData, + EqualsData, + SerialiseData, + // Misc constructors + // Constructors that we need for constructing e.g. Data. Polymorphic builtin + // constructors are often problematic (See note [Representable built-in + // functions over polymorphic built-in types]) + MkPairData, + MkNilData, + MkNilPairData, +} diff --git a/src/lib.rs b/src/lib.rs index 7378cc9d..8f60fde7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ pub mod ast; +pub mod builtins; pub mod cli; pub mod parser; diff --git a/src/parser.rs b/src/parser.rs index e8fbaeac..051f9dac 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -8,7 +8,10 @@ use combine::{ token, EasyParser, ParseError, Parser, Stream, }; -use crate::ast::{Constant, DefaultFunction, Program, Term}; +use crate::{ + ast::{Constant, Program, Term}, + builtins::DefaultFunction, +}; pub fn program(src: &str) -> anyhow::Result { let mut parser = program_(); @@ -167,8 +170,7 @@ where token(')'), string("error") .with(skip_many1(space())) - .with(term_()) - .map(|term| Term::Error(Box::new(term))), + .map(|_| Term::Error), ) }