# Copyright (c) 2014-2026, Chris Pressey, Cat's Eye Technologies.
# This file is distributed under a 2-clause BSD license, see LICENSES/ dir.
# SPDX-License-Identifier: LicenseRef-BSD-2-Clause-X-Tamsin
# Interpreter for "Micro-Tamsin", written in Tamsin.
# (see doc/Micro-Tamsin.md.)
# REQUIRES lib/tamsin_scanner.tamsin
# REQUIRES lib/tamsin_parser.tamsin
main = tamsin_parser:parse → AST & tamsin_scanner:skippable & "/" &
interpret(AST, AST).
interpret(P, program(L)) =
tamsin_parser:find_production_global('main', 'main', P) → Main &
interpret(P, Main).
interpret(P, production(N, list(prodbranch(Fs, Ls, E), nil))) = interpret(P, E).
interpret(P, call(prodref('$', 'return'), list(atom(X), nil))) = return X.
interpret(P, call(prodref('$', 'expect'), list(atom(X), nil))) = «X».
interpret(P, call(prodref('$', 'print'), list(atom(X), nil))) = print X.
interpret(P, call(prodref('', N), A)) =
interpret(P, call(prodref('main', N), A)).
interpret(P, call(prodref(M, N), A)) =
tamsin_parser:find_production_global(M, N, P) → Prod &
interpret(P, Prod).
interpret(P, or(L, R)) = interpret(P, L) | interpret(P, R).
interpret(P, and(L, R)) = interpret(P, L) & interpret(P, R).
interpret(P, not(X)) = !interpret(P, X).
interpret(P, while(X)) = {interpret(P, X)}.