Skip to main content

Built-in Functions

Circuitscript provides a set of built-in functions available in every script without any imports. These cover common operations like iteration, array manipulation, type conversion, and component inspection.


range

range(end)
range(start, end)

Returns an array of integers from start (inclusive, default 0) to end (exclusive). Typically used with for loops.

from "std" import *

vcc = supply("VCC")
gnd = dgnd()

at vcc
wire right 100

for i in range(0, 4):
add led("GREEN") pin 2
wire right 100

add res(10k)
wire right 100 down 100
to gnd
line 6:1VCCComponent D1_1: line 10:512D1_1GREENComponent D1_2: line 10:512D1_2GREENComponent D1_3: line 10:512D1_3GREENComponent D1_4: line 10:512D1_4GREENComponent R1: line 13:1R110kline 15:4GND

enumerate

enumerate(array)

Returns an array of [index, value] pairs from the input array. Useful when you need both the position and the value while iterating.

from "std" import *

vcc = supply("VCC")
gnd = dgnd()

at vcc
wire right 100

colors = ["yellow", "red", "blue", "green"]

for pair in enumerate(colors):
i = array_get(pair, 0)
color = array_get(pair, 1)
add led(color) pin 2
wire right 100

add res(10k)
wire right 100 down 100
to gnd
line 6:1VCCComponent D1_1: line 14:512D1_1yellowComponent D1_2: line 14:512D1_2redComponent D1_3: line 14:512D1_3blueComponent D1_4: line 14:512D1_4greenComponent R1: line 17:1R110kline 19:4GND

len

len(object)

Returns the number of elements in an array, or the number of keys in an object.


str

str(value)

Converts a value (number, boolean, etc.) to its string representation.


to_mils

to_mils(value)

Converts a metric value (millimetres) to mils (thousandths of an inch). Useful when working with layout coordinates that use imperial units.


array_push

array_push(array, value)

Appends value to the end of array and returns the array.

items = []
array_push(items, "a")
array_push(items, "b")
array_push(items, "c")
# items is now ["a", "b", "c"]

array_get

array_get(array, index)

Returns the element at index in array (zero-based).


array_set

array_set(array, index, value)

Sets the element at index in array to value and returns the array.

items = ["a", "b", "c"]
array_set(items, 1, "z")
# items is now ["a", "z", "c"]

pin_set_type

pin_set_type(type)
pin_set_type(pin_id, type)
pin_set_type(component, pin_id, type)

Sets the pin type of pin_id on component. Accepts both full type names and short-form aliases (e.g. "in", "out"). See the pin types reference for the list of valid values. The short forms operate on the cursor: pin_set_type(type) uses the current cursor component and pin, and pin_set_type(pin_id, type) uses the current cursor component with the given pin.


pin_get_type

pin_get_type()
pin_get_type(pin_id)
pin_get_type(component, pin_id)

Returns the current pin type string for pin_id on component. With no arguments, returns the type of the current cursor pin on the current cursor component; with a single pin_id argument, returns the type of that pin on the current cursor component.


has_pin

has_pin(pin_id)
has_pin(component, pin_id)

Returns true if component has a pin with the given ID, false otherwise. The single-argument form checks pin_id against the current cursor component. Unlike pin_set_type and pin_get_type, has_pin accepts only these two forms — any other number of arguments is an error.


net_get

net_get()
net_get(component)
net_get(component, pin_id)

Returns the net attached to a component pin. With no arguments, uses the current cursor component and pin. With only component given, uses that component's default pin. With both arguments, uses the given pin on the given component. Throws Invalid parameter for net_get function, expected a component if the first argument is not a component.


print

print(value, ...)

Prints one or more values to the interpreter output. Accepts any number of arguments. Useful for debugging scripts.