Skip to content

Commit

Permalink
init project from existing Nim code
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Feb 19, 2021
0 parents commit ce2b512
Show file tree
Hide file tree
Showing 51 changed files with 1,358 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

yarn.lock -diff linguist-generated
21 changes: 21 additions & 0 deletions .github/workflows/npm-publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: npm publish

on:
release:
types: [created]

jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/

- run: yarn && yarn test && yarn compile

- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
30 changes: 30 additions & 0 deletions .github/workflows/upload.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Upload Assets

on:
pull_request: {}
push:
branches:
- master

jobs:
upload-assets:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Use Node.js
uses: actions/setup-node@v1

- name: Get yarn cache
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v1
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- run: yarn && yarn test
name: Tests
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
node_modules/
typings/
.idea/
.vscode/
dist/
webpack/dll/*
.awcache/

.DS_Store
*.swp
*.log

.cache-loader/*
lib/*
13 changes: 13 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.cache-loader
.editorconfig
.gitattributes
.gitignore
.prettierrc
dist
example
template.ejs
tsconfig.json
webpack
yarn.lock
tests/
src/
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"tabWidth": 2,
"useTabs": false,
"printWidth": 120,
"trailingComma": "es5",
"arrowParens": "always"
}
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Cirru Writer in TypeScript

### Usages

_TODO_

### License

MIT
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@cirru/writer.ts",
"version": "0.1.0-a1",
"description": "Cirru Writer in TS",
"main": "lib/writer.js",
"author": "Cirru",
"license": "MIT",
"scripts": {
"compile": "tsc --outDir lib/",
"test": "ts-node tests/test-writer.ts"
},
"devDependencies": {
"@types/node": "^14.14.30",
"prettier": "^2.2.1",
"ts-node": "^9.1.1",
"typescript": "^4.1.5"
}
}
17 changes: 17 additions & 0 deletions src/from-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { CirruWriterNode, CirruWriterNodeKind } from "./types";

export function toWriterList(xs: any): CirruWriterNode {
if (typeof xs === "string") {
return {
kind: CirruWriterNodeKind.writerItem,
item: xs,
};
} else if (Array.isArray(xs)) {
return {
kind: CirruWriterNodeKind.writerList,
list: xs.map(toWriterList),
};
} else {
throw new Error("unexpected type to gen list");
}
}
16 changes: 16 additions & 0 deletions src/str-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function isADigit(c: string): boolean {
let n = c.charCodeAt(0);
// ascii table https://tool.oschina.net/commons?type=4
return n >= 48 && n <= 57;
}

export function isALetter(c: string): boolean {
let n = c.charCodeAt(0);
if (n >= 65 && n <= 90) {
return true;
}
if (n >= 97 && n <= 122) {
return true;
}
return false;
}
56 changes: 56 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export enum CirruWriterNodeKind {
writerItem,
writerList,
}

export type CirruWriterNode =
| {
kind: CirruWriterNodeKind.writerItem;
item: string;
}
| {
kind: CirruWriterNodeKind.writerList;
list: Array<CirruWriterNode>;
};

export enum WriterNodeKind {
writerKindNil = "nil",
writerKindLeaf = "leaf",
writerKindSimpleExpr = "simple",
writerKindBoxedExpr = "boxed",
writerKindExpr = "expr",
}

export function toString(xs: CirruWriterNode): string {
let result = "";
if (xs.kind == CirruWriterNodeKind.writerItem) {
return xs.item;
} else {
for (let idx = 0; idx < xs.list.length; idx++) {
let item = xs.list[idx];
if (idx > 0) {
result = result + " ";
}
if (item.kind == CirruWriterNodeKind.writerItem) {
result = result + item.item;
} else {
result = result + "(" + toString(item) + ")";
}
}
}
return result;
}

export function isSimpleExpr(xs: CirruWriterNode): boolean {
if (xs.kind == CirruWriterNodeKind.writerList) {
for (let x of xs.list) {
if (x.kind != CirruWriterNodeKind.writerItem) {
return false;
}
}

return true;
} else {
return false;
}
}
Loading

0 comments on commit ce2b512

Please sign in to comment.