From 3407b4d273624ea5976c57f3582b4d5483f2ffb2 Mon Sep 17 00:00:00 2001 From: Patrick Thomson Date: Tue, 17 Dec 2019 13:14:17 -0500 Subject: [PATCH 1/5] Introduce helper for preluded definitions. --- semantic-python/src/Language/Python/Core.hs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/semantic-python/src/Language/Python/Core.hs b/semantic-python/src/Language/Python/Core.hs index f3ca48783..37d0120d9 100644 --- a/semantic-python/src/Language/Python/Core.hs +++ b/semantic-python/src/Language/Python/Core.hs @@ -46,6 +46,9 @@ pattern SingleIdentifier name <- Py.ExpressionList ] } +prelude :: Has Core sig t => [Name] -> t Name +prelude = foldl' (\a b -> a ... b) (pure "__semantic_prelude") + -- We leave the representation of Core syntax abstract so that it's not -- possible for us to 'cheat' by pattern-matching on or eliminating a -- compiled term. From 228e3470e9107809ec8d4e32256f1c02f70b985c Mon Sep 17 00:00:00 2001 From: Patrick Thomson Date: Tue, 17 Dec 2019 13:14:30 -0500 Subject: [PATCH 2/5] Use preluded helper. --- semantic-python/src/Language/Python/Core.hs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/semantic-python/src/Language/Python/Core.hs b/semantic-python/src/Language/Python/Core.hs index 37d0120d9..1035ac406 100644 --- a/semantic-python/src/Language/Python/Core.hs +++ b/semantic-python/src/Language/Python/Core.hs @@ -199,8 +199,8 @@ instance Compile Py.ClassDefinition where bindings <- asks @Bindings (toList . unBindings) let buildName n = (n, pure n) contents = record . fmap buildName $ bindings - typefn = pure "__semantic_prelude" ... "type" - object = pure "__semantic_prelude" ... "object" + typefn = prelude ["type"] + object = prelude ["object"] pure (typefn $$ Core.string (coerce n) $$ object $$ contents) @@ -346,8 +346,8 @@ instance Compile Py.Module where instance Compile Py.NamedExpression instance Compile Py.None where - -- None is not overridable, and thus always points to the prelude's None. - compile _it cc _ = cc (pure "__semantic_prelude" ... "None") + -- None is not an lvalue, and thus always points to the prelude's None. + compile _it cc _ = cc (prelude ["None"]) instance Compile Py.NonlocalStatement instance Compile Py.NotOperator @@ -387,7 +387,7 @@ instance Compile Py.String where if any isNothing contents then pure . invariantViolated $ "Couldn't string-desugar " <> show it - else let new = pure "__semantic_prelude" ... "str" ... "__slots" ... "__new__" + else let new = prelude ["str", "__slots", "__new__"] in cc $ locate it (new $$ Core.string (mconcat (catMaybes contents))) instance Compile Py.Subscript From e7c7910760c5302ac5e7ae20f1e297b1cb79c0b6 Mon Sep 17 00:00:00 2001 From: Patrick Thomson Date: Tue, 17 Dec 2019 13:22:36 -0500 Subject: [PATCH 3/5] Add provisional __not__ definition. --- semantic-python/src/Prelude.score | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/semantic-python/src/Prelude.score b/semantic-python/src/Prelude.score index 775f96b3a..53130556e 100644 --- a/semantic-python/src/Prelude.score +++ b/semantic-python/src/Prelude.score @@ -10,6 +10,10 @@ str <- type "str" object #record { __new__: \prim -> instance #unit prim #record{} }; + // We will fill in the actual definition of these operators + // pending the presence of more eliminators. + not <- \val -> if val then #false else #true; + NoneType <- type "None" object #record { __new__: \prim -> instance #unit prim #record{} }; None <- NoneType.__slots.__new__ #unit; @@ -19,6 +23,7 @@ #record { type: type , object: object , str: str + , not: not , NoneType: NoneType , None: None , getitem: getitem} From 9d0afb4f0a56fc129b1f5099c0a1356b8695499c Mon Sep 17 00:00:00 2001 From: Patrick Thomson Date: Tue, 17 Dec 2019 13:25:59 -0500 Subject: [PATCH 4/5] Compile not statements. --- semantic-python/src/Language/Python/Core.hs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/semantic-python/src/Language/Python/Core.hs b/semantic-python/src/Language/Python/Core.hs index 1035ac406..556cd03ce 100644 --- a/semantic-python/src/Language/Python/Core.hs +++ b/semantic-python/src/Language/Python/Core.hs @@ -350,7 +350,11 @@ instance Compile Py.None where compile _it cc _ = cc (prelude ["None"]) instance Compile Py.NonlocalStatement -instance Compile Py.NotOperator + +instance Compile Py.NotOperator where + compile _it@Py.NotOperator{ argument } cc next = do + val <- compile argument pure next + cc (prelude ["not"] $$ val) instance Compile Py.ParenthesizedExpression where compile it@Py.ParenthesizedExpression { extraChildren } cc From 74183dba8c14ef847ad5f6c921b2b31e8d839d8e Mon Sep 17 00:00:00 2001 From: Patrick Thomson Date: Tue, 17 Dec 2019 13:26:17 -0500 Subject: [PATCH 5/5] Add a test. --- semantic-python/test/fixtures/4-03-not-expression.py | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 semantic-python/test/fixtures/4-03-not-expression.py diff --git a/semantic-python/test/fixtures/4-03-not-expression.py b/semantic-python/test/fixtures/4-03-not-expression.py new file mode 100644 index 000000000..47c903f41 --- /dev/null +++ b/semantic-python/test/fixtures/4-03-not-expression.py @@ -0,0 +1,2 @@ +# CHECK-TREE: { x <- __semantic_prelude.not #true; #record { x: x }} +x = not True