Files
2026-02-19 16:16:24 -05:00

13 KiB
Raw Permalink Blame History

luaforth

Source: https://github.com/vifino/luaforth

GitHub - vifino/luaforth: A simple Forth in Lua for embedded usage.

Skip to content

Navigation Menu

Toggle navigation

Sign in

Appearance settings

Search or jump to...

Search code, repositories, users, issues, pull requests...

Search

Clear

Search syntax tips

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Cancel Submit feedback

Saved searches

Use saved searches to filter your results more quickly

Name

Query

To see all available qualifiers, see our documentation.

Cancel Create saved search

Sign in

Sign up

Appearance settings

Resetting focus

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Dismiss alert

{{ message }}

vifino / luaforth Public

A simple Forth in Lua for embedded usage.

License

MIT license

6 stars 2 forks Branches Tags Activity

Star

Notifications You must be signed in to change notification settings

Additional navigation options

vifino/luaforth

master

BranchesTags

Go to file

Code

Open more actions menu

Folders and files

Name Name Last commit message Last commit date
Latest commit History27 Commits 27 Commits
bin bin
tests tests
.travis.yml .travis.yml
LICENSE LICENSE
Makefile Makefile
README.md README.md
app.lua app.lua
auto-run-tests-on-change.sh auto-run-tests-on-change.sh
luaforth.lua luaforth.lua
View all files

Repository files navigation

luaforth

Build Status Coverage Status

A simplistic and decently fast base implementation of a Forth parser.

If you expect a fully featured forth here, you're wrong.

This is made for people who want to embed a Forth-like into their project.

Usage

  1. require/load luaforth.
  2. Create an environment.
  3. Call new_stack, new_environment = luaforth.eval(program_source, environment[, stack, program_source_start_position]).

Tada!

Example

See luaforth.simple_env here or below.

-- Example env that has %L to evaluate the line and [L L] pairs to evaluate a small block of Lua code.
local simple_env = {
	["%L"] = {
		_fn=function(stack, env, str)
			local f, err = loadstring("return " .. str)
			if err then
				f, err = loadstring(str)
				if err then
					error(err, 0)
				end
			end
			return f()
		end,
		_parse = "line"
	},
	["[L"] = {
		_fn=function(stack, env, str)
			local f, err = loadstring("return " .. str)
			if err then
				f, err = loadstring(str)
				if err then
					error(err, 0)
				end
			end
			return f()
		end,
		_parse = "endsign",
		_endsign = "L]"
	}
}

-- Function creation.
luaforth.simple_env[":"] = {
	_fn = function(stack, env, fn)
		local nme, prg = string.match(fn, "^(.-) (.-)$")
		luaforth.simple_env[nme] = {
			_fn = function(stack, env)
				return luaforth.eval(prg, env, stack)
			end,
			_fnret = "newstack"
		}
	end,
	_parse = "endsign",
	_endsign = ";"
}

Environment

Contains words, strings, booleans, numbers and other things that the forth instance will be able to use.

Word Structure

Words are Forth jargon for functions.

Look here or below to see how they are structured in this implementation.

-- Word structure:
-- env[name] = {
--  _fn = func -- function that runs the logic
--  _fnret = ["pushtostack", "newstack"] -- wether the function's return values should be added to the stack or _be_ the stack. Defaults to pushtostack.
--  _args = n -- number of arguments which are pop'd from the stack, defaults to 0
--  _parse = ["line"|"word"|"endsign"|"pattern"] -- optional advanced parsing, line passes the whole line to the word, word only the next word, pattern parses given pattern, endsign until...
--  _endsign = string -- the given endsign appears.
--  _pattern = pattern -- pattern for parse option
-- }

License

MIT

About

A simple Forth in Lua for embedded usage.

Topics

lua-library lua forth

Resources

Readme

License

MIT license

Uh oh!

There was an error while loading. Please reload this page.

Activity

Stars

6 stars

Watchers

2 watching

Forks

2 forks

Report repository

Releases

No releases published

Packages 0

No packages published

Contributors 2

Uh oh!

There was an error while loading. Please reload this page.

Languages

© 2026 GitHub, Inc.

You cant perform that action at this time.