Skip to content

Schachte/Tartufo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TuffieLang

WIP interpreter for a dynamic scripting language built using Typescript to play around with lexers and parsers.

Tests

npm run test

Usage

import { Lexer } from "./lexer";
import { Parser } from "./parser";

const sourceCode = `
let hello = 2; 
let test = 'sup';
`;

const lexer = new Lexer(sourceCode);
const tokens = lexer.tokenize();

const parser = new Parser(tokens);
const program = parser.parse();

for (const node of program.body) {
  console.log(
    `AST: ${JSON.stringify(node, null, 2)}\nSerialized output: ${
      node.debug && node.debug()
    }\n`
  );
}

REPL Demo

──────▄▀▄─────▄▀▄
─────▄█░░▀▀▀▀▀░░█▄
─▄▄──█░░░░░░░░░░░█──▄▄
█▄▄█─█░░▀░░┬░░▀░░█─█▄▄█
-----------------------
--    TUFFIE LANG    --
-----------------------
(Interactive REPL v1.0)

  <ctrl> + c to exit


🐈 > let dat=6+6*2/9;
{ 
  variables: [
    {
      dat: 7.333333333333333
    }
  ]
}

Output

AST: {
  "type": "LetStatement",
  "identifier": "hello",
  "expression": {
    "type": "NumberExpression",
    "literal": 2
  }
}
Serialized output: let hello = 2;

AST: {
  "type": "LetStatement",
  "identifier": "test",
  "expression": {
    "type": "StringExpression",
    "literal": "sup"
  }
}
Serialized output: let test = 'sup';