SlideShare uma empresa Scribd logo
1 de 100
Baixar para ler offline
Implementasi VendisScript
di Python
Oleh: Irsyad Asyhari Lubis
Apa itu VendisScript?
is a scripting language
A scripting language or script language is a
programming language that supports the writing
of scripts, programs written for a special runtime
environment that can interpret and automate the
execution of tasks which could alternatively be
executed one-by-one by a human operator.
Sumber: http://en.wikipedia.org/wiki/Scripting_language
Typically, a scripting language is characterized by
the following properties:
● Ease of use.
● OS facilities - especially filesystem and related,
built in with easy interfaces.
● Interpreted from source code - to give the
fastest turnaround from script to execution.
● Relatively loose structure.
Sumber: http://en.wikipedia.org/wiki/Scripting_language
Typically, a scripting language is characterized by
the following properties:
● Ease of use.
● OS facilities - especially filesystem and related,
built in with easy interfaces.
● Interpreted from source code - to give the
fastest turnaround from script to execution.
● Relatively loose structure.
Sumber: http://en.wikipedia.org/wiki/Scripting_language
is interpreted scripting language
An interpreted language is a programming
language that avoids explicit program compilation.
The interpreter executes the program source code
directly, statement by statement, as a processor
or scripting engine does. This can be contrasted
with compiled language programs, which the user
must explicitly translate into a lower-level machine
language executable.
Sumber: http://en.wikipedia.org/wiki/Interpreted_language
is embedded, interpreted scripting language
sorry, no reference this time...
used in Android based mobile sales and
distribution software
why not Python?
compared to Python, VendisScript is...
simple
in term of implementation
too simple...
no optimization
no optimization (yet)
but, there is one reason to rule them all
size
it does matter!
less why...
more how...
now we are talking about implementation
in Python
note that...
scripting language on top of Python is generally
not necessary
unless it is DSL
since this is only emulator...
Apa itu VendisScript?
JSON + Lisp
JSON
{
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": 10021
    },
    "phoneNumbers": [
        {
            "type": "home",
            "number": "212 555­1234"
        },
        {
            "type": "fax",
            "number": "646 555­4567"
        }
    ]
}
Sumber: http://en.wikipedia.org/wiki/JSON
Lisp
(defun factorial (n)
   (if (<= n 1)
       1
       (* n (factorial (­ n 1)))))
Sumber: https://en.wikipedia.org/wiki/Lisp_(programming_language)
Basic syntax
[
{
  "d": "Gratis 5 produk A untuk setiap pembelian 100 produk A.",
  "i": ["and",
         ["has", "11112313", "@products"],
         [">=", 
           ["bulk­qty", ["take", "11112313", "@products"]], 
           100.0]],
  "o": ["setq", "bonuses",
         ["add­bonus",
           ["bonus­new", "11112313",
             ["floatp", ["*", ["intp", 
               ["/", ["bulk­qty", ["take", "11112313", 
"@products"]], 100.0]], 5]], 
             "PCS"], 
             "@bonuses"]]
}
]
[
{
  "d": "Gratis 5 produk A untuk setiap pembelian 100 produk A.",
  "i": ["and",
         ["has", "11112313", "@products"],
         [">=", 
           ["bulk­qty", ["take", "11112313", "@products"]], 
           100.0]],
  "o": ["setq", "bonuses",
         ["add­bonus",
           ["bonus­new", "11112313",
             ["floatp", ["*", ["intp", 
               ["/", ["bulk­qty", ["take", "11112313", 
"@products"]], 100.0]], 5]], 
             "PCS"], 
             "@bonuses"]]
}
]
{
"d": "Test factorial.",
"i": true,
"o": ["prog",
["setq", "fac", 
["lambda", ["x"],
["cond",
["<=", "@x", 1], 1,
true, ["*", ["fac", ["­", "@x", 1]], "@x"]]]],
["fac", 3]]
}
{
"d": "Test factorial.",
"i": true,
"o": ["prog",
["setq", "fac", 
["lambda", ["x"],
["cond",
["<=", "@x", 1], 1,
true, ["*", ["fac", ["­", "@x", 1]], "@x"]]]],
["fac", 3]]
}
def fac(x):
if x <= 1:
return 1
else:
return x * fac(x ­ 1) 
[
{
“d”: @description(str),
“i”: @input(JSON),
“o”: @output(JSON)
},
...
]
Demo
Compiler Stack
Source
Code
Lexer Parser AST
Target
Code
Grammar
prog : ('[' item (',' item)* ']' | '[' ']') EOF;
item : '{' '"d"' ':' QUOTED_IDENTIFIER ',' '"i"' ':' expr ',' 
'"o"' ':' expr '}';
expr :
     | lambda_expr
     | let_expr
     | setq_expr
     | prog_expr
     | cond_expr
     | apply_expr
     | QUOTED_IDENTIFIER
     | VAR_ACCESS
     | '#nil'
     | INT
     | FLOAT
     | 'true'
     | 'false'
     ;
lambda_expr : '[' '"lambda"' ',' '[' params ']' ',' body ']';
params : QUOTED_IDENTIFIER (',' QUOTED_IDENTIFIER)*;
body : expr ;
let_expr : '[' '"let"' ',' '[' init_list* ']' ',' body ']';
setq_expr : '[' '"setq"' ',' QUOTED_IDENTIFIER ',' expr ']';
init_list : '[' QUOTED_IDENTIFIER ',' expr ']';
prog_expr : '[' '"prog"' (',' expr)+ ']';
cond_expr : '[' '"cond"' (',' cond_and_expr)+ ']';
cond_and_expr : expr ',' expr;
apply_expr : '[' QUOTED_IDENTIFIER (',' expr)* ']';
VAR_ACCESS : '"@' IDENTIFIER '"';
QUOTED_IDENTIFIER : '"' IDENTIFIER '"';
INT : '­'? INT_WITHOUT_PREFIX;
FLOAT : '­'? INT_WITHOUT_PREFIX '.' [0­9]* EXP?;
WS : [ tnr]+ ­> skip;
fragment
IDENTIFIER : (ESC | ~('"'|''))*;
fragment
INT_WITHOUT_PREFIX : '0'|[1­9][0­9]*;
fragment
EXP : [Ee][+|­]? INT_WITHOUT_PREFIX;
fragment
ESC : '' ('b'|'t'|'n'|'f'|'r'|'"'|''|UNICODE);
fragment
UNICODE : 'u' HEX HEX HEX HEX;
fragment
HEX : [0­9a­fA­F];
Lexer
JSON's lexer + parser
Parser
AST
1 + (2 * 3)
["+", 1, ["*", 2, 3]]
+
1 *
2 3
class Expression(object):
def __init__(self, args=None):
self.args = args if args else []
def evaluate(self, env):
pass
+
1 *
2 3
AddExpression
MulExpressionIntExpression
IntExpression IntExpression
class IntExpression(Expression):
    def __init__(self, value):
        self.value = value
    def evaluate(self, env):
        return self.value
class AddExpression(BasicMathExpression):
    def evaluate(self, env):
        return self._evaluate(env, 'Add', lambda x, y: x + y)
class MulExpression(BasicMathExpression):
    def evaluate(self, env):
        return self._evaluate(env, 'Mul', lambda x, y: x * y)
Tipe data
✔ Integer → ­1 0 1 2
✔ Float → ­2.0 1.2 2.3
✔ String → "This is a string"
✔ List → ["list", 1, 2, 3.0, 
 "This is a string", 
["list", 2, 3]]
✔ Empty list → "#nil"
✔ Boolean → true false
✔ Function
✔ Product
Cons
["cons", 1, ["cons", 2, ["cons", 3, "#nil"]]]
["list", 1, 2, 3]
( head tail )
( head ( head tail ) )
( head ( head ( head tail ) ) )
( head ( head ( head nil ) ) )
class Cons(object):
    def __init__(self, head=None, tail=None):
        self._head = head
        self._tail = tail
    @property
    def head(self):
        return self._head
    @property
    def tail(self):
        return self._tail
    @staticmethod
    def from_seq(seq):
        if not seq:
            return None
        head = Cons(seq[0])
        current = head
        for item in seq[1:]:
            next_cons = Cons(item)
            current._tail = next_cons
            current = next_cons
        return head
    def to_list(self):
        result = []
        self.each(result.append)
        return result
    def each(self, f):
        f(self.head)
        tail = self.tail
        while tail != None:
            if isinstance(tail, Cons):
                f(tail.head)
                tail = tail.tail
            else:
                f(tail)
                tail = None
def map(self, f):
pass
def filter(self, f):
pass
def reduce(self, f, *args):
pass
Scope
Dynamic Scope vs Lexical Scope
Dynamic Scope vs Lexical Scope
two constructs to introduce a variable
["setq", "variable1", 100]
["let", [["variable1", 100]]
["print", "@variable1"]]
function's paramenters also introduce local
variables
{
"d": "Test factorial.",
"i": true,
"o": ["prog",
["setq", "fac", 
["lambda", ["x"],
["cond",
["<=", "@x", 1], 1,
true, ["*", ["fac", ["­", "@x", 1]], "@x"]]]],
["fac", 3]]
}
class BonusEnvironment(dict):
    def __init__(self, parent=None):
        self._parent = parent
    def __getitem__(self, key):
        if key not in self and self._parent:
            return self._parent[key]
        else:
            return super(BonusEnvironment, self).__getitem__(key)
    def set_global(self, key, value):
        # by convention, global environment is whose parent 
        # is None.
        if not self._parent:
            self[key] = value
        else:
            self._parent.set_global(key, value)
class GetValExpression(Expression):
    def evaluate(self, env):
        name = self._evaluated_args(env, 
                                    'GetVal', ((str, unicode),))
        try:
            return env[name]
        except KeyError:
            # search in builtin functions
            try:
                builtin_class_name = builtin_expressions[name]
                builtin_class = globals()[builtin_class_name]
                builtin_instance = builtin_class()
                return BuiltInFunction(body=builtin_instance)
            except KeyError:
                return None
Functions
63 builtin functions
["setq", "fib",
["lambda", ["n"],
["cond",
["<", "@n", 2], 1,
true, ["+", ["fib", ["­", "@n", 1]],
["fib", ["­", "@n", 2]]]]]]
class LambdaExpression(Expression):
    def __init__(self, params, body):
        self.params = params
        self.body = body
    def evaluate(self, env):
        return Function(self.params, self.body)
class ApplyExpression(Expression):
    def __init__(self, funcName, args):
        self.funcName = funcName
        self.args = args
    def evaluate(self, env):
        func = env[self.funcName]
        if isinstance(func, Function):
            return func.apply(env, self.args)
        else:
            raise ExpressionException('Apply: Cannot find function 
with name {0}'.format(self.funcName))
how to add new builtin function?
it should be easy, right?
Demo
Regrets
should not use None as value
class NilType(object):
def __nonzero__(self):
return False
Nil = NilType()
Thank you!!!

Mais conteúdo relacionado

Semelhante a Implementasi VendisScript di Python

Specification Of The Programming Language Of Java
Specification Of The Programming Language Of JavaSpecification Of The Programming Language Of Java
Specification Of The Programming Language Of Java
Kim Moore
 

Semelhante a Implementasi VendisScript di Python (20)

COMPUTER LANGUAGES AND THERE DIFFERENCE
COMPUTER LANGUAGES AND THERE DIFFERENCE COMPUTER LANGUAGES AND THERE DIFFERENCE
COMPUTER LANGUAGES AND THERE DIFFERENCE
 
Entrepreneur’s guide to programming
Entrepreneur’s guide to programmingEntrepreneur’s guide to programming
Entrepreneur’s guide to programming
 
PPT ON PROGRAMMING LANGUAGES AN THEIR TYPES.ppt
PPT ON PROGRAMMING LANGUAGES AN THEIR TYPES.pptPPT ON PROGRAMMING LANGUAGES AN THEIR TYPES.ppt
PPT ON PROGRAMMING LANGUAGES AN THEIR TYPES.ppt
 
The Ring programming language version 1.10 book - Part 6 of 212
The Ring programming language version 1.10 book - Part 6 of 212The Ring programming language version 1.10 book - Part 6 of 212
The Ring programming language version 1.10 book - Part 6 of 212
 
Python | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialPython | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python Tutorial
 
The Ring programming language version 1.5.3 book - Part 5 of 184
The Ring programming language version 1.5.3 book - Part 5 of 184The Ring programming language version 1.5.3 book - Part 5 of 184
The Ring programming language version 1.5.3 book - Part 5 of 184
 
Phython Programming Language
Phython Programming LanguagePhython Programming Language
Phython Programming Language
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
637b4894085c4_ppt.pptx
637b4894085c4_ppt.pptx637b4894085c4_ppt.pptx
637b4894085c4_ppt.pptx
 
Python tutorial for beginners - Tib academy
Python tutorial for beginners - Tib academyPython tutorial for beginners - Tib academy
Python tutorial for beginners - Tib academy
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners
 
intro.pptx (1).pdf
intro.pptx (1).pdfintro.pptx (1).pdf
intro.pptx (1).pdf
 
The Ring programming language version 1.9 book - Part 6 of 210
The Ring programming language version 1.9 book - Part 6 of 210The Ring programming language version 1.9 book - Part 6 of 210
The Ring programming language version 1.9 book - Part 6 of 210
 
The Ring programming language version 1.5.4 book - Part 5 of 185
The Ring programming language version 1.5.4 book - Part 5 of 185The Ring programming language version 1.5.4 book - Part 5 of 185
The Ring programming language version 1.5.4 book - Part 5 of 185
 
Specification Of The Programming Language Of Java
Specification Of The Programming Language Of JavaSpecification Of The Programming Language Of Java
Specification Of The Programming Language Of Java
 
Unit 1-introduction to scripts
Unit 1-introduction to scriptsUnit 1-introduction to scripts
Unit 1-introduction to scripts
 
The Ring programming language version 1.5.2 book - Part 5 of 181
The Ring programming language version 1.5.2 book - Part 5 of 181The Ring programming language version 1.5.2 book - Part 5 of 181
The Ring programming language version 1.5.2 book - Part 5 of 181
 
Top Programming Languages of 2020
Top Programming Languages of 2020Top Programming Languages of 2020
Top Programming Languages of 2020
 
CSC1100 - Chapter11 - Programming Languages and Program Development
CSC1100 - Chapter11 - Programming Languages and Program DevelopmentCSC1100 - Chapter11 - Programming Languages and Program Development
CSC1100 - Chapter11 - Programming Languages and Program Development
 
Introduction python
Introduction pythonIntroduction python
Introduction python
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Implementasi VendisScript di Python