1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 17:04:47 +03:00

Merge pull request #2044 from github/js-exports

Eval `export function ...` in JavaScript
This commit is contained in:
Rick Winfrey 2018-07-13 10:11:23 -07:00 committed by GitHub
commit 989cf32231
4 changed files with 16 additions and 2 deletions

View File

@ -10,7 +10,7 @@ import Prologue
import Proto3.Suite.Class
data Function a = Function { functionContext :: ![a], functionName :: !a, functionParameters :: ![a], functionBody :: !a }
deriving (Eq, Ord, Show, Foldable, Traversable, Functor, Generic1, Hashable1, Mergeable, FreeVariables1, Declarations1, ToJSONFields1, Named1, Message1)
deriving (Eq, Ord, Show, Foldable, Traversable, Functor, Generic1, Hashable1, Mergeable, FreeVariables1, ToJSONFields1, Named1, Message1)
instance Diffable Function where
equivalentBySubterm = Just . functionName
@ -33,6 +33,11 @@ instance Evaluatable Function where
instance Declarations a => Declarations (Function a) where
declaredName Function{..} = declaredName functionName
instance Declarations1 Function where
liftDeclaredName declaredName Function{..} =
case declaredName functionName of
[] -> Nothing
(x:_) -> Just x
data Method a = Method { methodContext :: ![a], methodReceiver :: !a, methodName :: !a, methodParameters :: ![a], methodBody :: !a }
deriving (Eq, Ord, Show, Foldable, Traversable, Functor, Generic1, Hashable1, Mergeable, FreeVariables1, Declarations1, ToJSONFields1, Named1, Message1)

View File

@ -270,10 +270,10 @@ instance Show1 DefaultExport where liftShowsPrec = genericLiftShowsPrec
instance Evaluatable DefaultExport where
eval (DefaultExport term) = do
v <- subtermValue term
case declaredName term of
Just name -> do
addr <- lookupOrAlloc name
v <- subtermValue term
assign addr v
export name name Nothing
bind name addr

View File

@ -0,0 +1,6 @@
export function square(x) {
return x * x;
}
export function area(x, y) {
return x * y;
}

View File

@ -0,0 +1,3 @@
import { square, area } from 'lib';
console.log(square(11)); // 121
console.log(area(4, 3)); // 12