Skip to content

Instantly share code, notes, and snippets.

View Heimdell's full-sized avatar
🔨
Right tool for the right job

Андреев Кирилл Heimdell

🔨
Right tool for the right job
  • Ульяновск
View GitHub Profile
@Heimdell
Heimdell / Parser.hs
Last active August 4, 2017 22:22
Parser for a Llama language + sample code
module Parser (toplevel, parse, previewError) where
import Control.Applicative
import Control.Monad (guard)
import Data.List (intercalate)
import Text.Parsec.Error
import Text.Parsec.Pos
<body>
<pre id="canvas">
<font caption="inconsolata">
Press 'space' to start.
'WASD' to move.
</font>
</pre>
<script type="text/javascript" src="./frp.js"></script>
<script type="text/javascript" src="./game.js"></script>
<script type="text/javascript" src="./logic.js"></script>
import Dict exposing (Dict)
import Graphics.Collage as Collage exposing (Form)
import Graphics.Element as Element exposing (Element)
import Keyboard
import Signal
import Time
import String
import Text
@Heimdell
Heimdell / lam.js
Last active July 1, 2017 12:41
Lazy Abstract Machine
/*
This is a Lazy Abstract Machine (LAM).
This code is meant to be glued before output of the compilator
(or `let {Node, func, call, etc...} = require('runtime.js')`)
- it depends on how will I implement a compiler.
It allows representing lazy programs for the lazy language with:
1) Push { elem: 1, to: Empty } - Algebraic datatypes
var assert = require('assert')
/*
LAM - Lazy Abstract Machine.
It evaluates the tree built from AST descendants lazily.
No optimization is performed (and minded - it should be done in compile-phase).
Call `run(ast)` to reduce it to Weak-Head Normal Form (go google it).
load(File, SExp)
:- read_file_to_string(File, Text, [])
, string_chars(Text, Atoms)
, phrase(entry(SExp), Atoms, [])
, !
.
entry(Res) --> spaces, many(sexp, Res).
data Iter = Iter
{ pos, col, line :: Int
}
data SExp = Block [SExp] | Atom String
deriving Show
parseSexp text =
let
@Heimdell
Heimdell / core-syntax.js
Last active May 1, 2017 15:02
Stack machine in js
var { Parser: P } = require("./parser-combinators.js")
var t = (s) => P.string(s).and_(spaces)
var nameChar = P.oneOf("qwertyuiopasdfghjklzxcvbnm1234567890-=+~:<>.,?/\\*#$%^!@|&{}[]")
var nameChar1 = P.oneOf("qwertyuiopasdfghjklzxcvbnm-=+~:<>.,?/\\*#$%^!@|&{}[]")
var comment = P.string(";")._and(P.anyChar().butNot(P.string("\n")).many())._and(P.string("\n"))
// set :: string -> (char -> bool)
var set = (items) => {
var arr = Array.prototype.slice.apply(items)
var carrier = {}
arr.forEach(x => { carrier[x] = true })
return (x) => carrier[x]
}
var reserved = set("module object end => let in \\ open ---".split(" "))
// 111.11
// 'hello '' world'
class Parser {
static of(parser) {
return new Parser(parser)
}
constructor(runParser) {
this.runParser = runParser