Language Guide

Scriptling is a dynamically-typed, interpreted language with Python-inspired syntax designed for embedding in Go applications.

Quick Reference

Variables and Types

# Variables
x = 10
name = "Alice"
price = 3.14

# Booleans and None
flag = True
done = False
result = None

# Lists and Dictionaries
nums = [1, 2, 3]
data = {"key": "value"}
first = nums[0]
val = data["key"]

# Sets
numbers = set([1, 2, 3])
unique = set([1, 2, 2, 3])  # {1, 2, 3}

Operators

# Arithmetic
+, -, *, /, //, %, **

# Comparison
==, !=, <, >, <=, >=

# Boolean/Logical
and, or, not

# Bitwise
&, |, ^, ~, <<, >>

# Augmented Assignment
+=, -=, *=, /=, //=, %=, &=, |=, ^=, <<=, >>=

# Chained comparisons
1 < x < 10        # Equivalent to: 1 < x and x < 10

Control Flow

# If/Elif/Else
if x > 10:
    print("large")
elif x > 5:
    print("medium")
else:
    print("small")

# While Loop
while x > 0:
    x -= 1

# For Loop
for item in [1, 2, 3]:
    if item == 2:
        continue  # Skip 2
    print(item)

# Match statement (pattern matching)
match status:
    case 200:
        print("Success")
    case 404:
        print("Not found")
    case _:
        print("Other")

Functions

# Definition
def add(a, b):
    return a + b

# Default parameters
def greet(name, greeting="Hello"):
    return greeting + ", " + name

# Variadic arguments (*args)
def sum_all(*args):
    total = 0
    for num in args:
        total += num
    return total

# Keyword arguments collection (**kwargs)
def test_kwargs(**kwargs):
    return kwargs

result = test_kwargs(a=1, b=2)  # {"a": 1, "b": 2}

# Lambda
square = lambda x: x * 2
sorted(["ccc", "a", "bb"], key=lambda s: len(s))

Error Handling

# Try/Except/Finally
try:
    result = risky_operation()
except:
    result = None
finally:
    cleanup()

# Raise errors
if x < 0:
    raise "Invalid value"

# Assert
assert x > 0, "x must be positive"

Classes

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        return "Hello, my name is " + self.name

# Inheritance with super()
class Dog(Animal):
    def __init__(self, name, breed):
        super(Dog, self).__init__(name)
        self.breed = breed

HTTP and JSON

import json
import requests

# HTTP GET
options = {"timeout": 10, "headers": {"Authorization": "Bearer token"}}
resp = requests.get("https://api.example.com/data", options)
if resp.status_code == 200:
    data = json.loads(resp.body)

# HTTP POST
payload = {"name": "Alice"}
body = json.dumps(payload)
response = requests.post("https://api.example.com/users", body)

Detailed Topics

Key Differences from Python

  • No nested classes
  • No multiple inheritance
  • HTTP response is a Response object with status_code, body, headers, url fields
  • Default HTTP timeout: 5 seconds
  • Use import library to load libraries dynamically

See Also