### This is the example of how powerful PatEL's macros are.
### We can implement the key logic of literate programming by using macros.
### /------------ THESE ARE THE CODE GENERATED BY THE TANGLER ----------------\

define-macro define-scope : syntax-rules
	`[define-scope @_name @form] [atom _name] : begin
		local name : formOf _name
		if [not externEnv.scopes] : set externEnv.scopes {.}
		set externEnv.scopes.(name) [formOf form]
		local defineStatement `[define-macro @name [syntax-rules \\
			`[@2*name @::args] [ex externEnv.scopes.(@{'.quote' name}) env]
		]]
		[env.macros.get 'define-macro'] defineStatement env
		return `nothing

define-macro append-scope : syntax-rules
	`[append-scope @_name @::body] [atom _name] : begin
		local name : formOf _name
		if [not externEnv.scopes] : set externEnv.scopes {.}
		if externEnv.scopes.(name) : begin
			set externEnv.scopes.(name)
				externEnv.scopes.(name).concat : body.map formOf
		return `nothing

define-scope root : begin
	sayhello
	domath

### \------------------------------ GENERATED --------------------------------/

### /------------------------------- WRITTEN ---------------------------------\

# Here we will implement a module which returns the number of CPUs on your
# machine. A common module structure looks like this:
define-scope root : begin
	import-part
	body-part
	export-part
define-scope import-part : begin
define-scope body-part : begin
define-scope export-part : begin

# In this module we will export a function which provides us the quantity of
# CPUs in the computer. To achirve this we have to import 'os'
append-scope import-part
	import 'os' as os

# The `os.cpus` provides a list of CPUs installed in the computer. We need its
# length only.
append-scope body-part
	define [ncpus] : begin
		return [os.cpus].length

# That's OK. Export it.
append-scope export-part
	export ncpus as ncpus

### \------------------------------- WRITTEN ---------------------------------/

### /------------------------------ GENERATED --------------------------------\
root
### \-------------------------------------------------------------------------/