Skip to content

Commit

Permalink
tree-sitter-swift
Browse files Browse the repository at this point in the history
  • Loading branch information
lancejpollard committed Jan 19, 2024
1 parent 87bc2a4 commit d3e179f
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 0 deletions.
68 changes: 68 additions & 0 deletions code/node/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,76 @@ import {
FormatRuby,
FormatAssembly,
FormatC,
CompileToAst,
} from '../shared/index.js'
import { handleCommand } from './command.js'
import Parser from 'tree-sitter'
import JavaScript from 'tree-sitter-javascript'
import Swift from 'tree-sitter-swift'
import fsp from 'fs/promises'

// compileToAsm({
// input: {
// format: 'js',
// file: { path: 'test/file/code/quicksort/quicksort.js' },
// },
// })

// compileToAsm({
// input: {
// format: 'js',
// file: { path: 'test/file/code/quicksort/quicksort.swift' },
// },
// })

const TREE_SITTER_LANGUAGE: Record<string, any> = {
js: JavaScript,
swift: Swift,
}

export async function compileToAsm(input: CompileToAst) {
const parser = new Parser()
parser.setLanguage(TREE_SITTER_LANGUAGE[input.input.format])
const code = await fsp.readFile(input.input.file.path, 'utf-8')
const tree = parser.parse(code)
return traverse(tree.rootNode)

function traverse(node) {
const out: any = {
form: node.type,
base: node.startPosition,
head: node.endPosition,
}
if (node.type.match(/^[\w_]+$/)) {
switch (node.type) {
case 'identifier':
case 'number':
case 'string':
case 'comment':
case 'string_fragment':
case 'template_string':
case 'simple_identifier':
case 'var':
case 'let':
case 'const':
case 'true':
case 'return':
case 'false':
out.text = node.text
break
}
} else {
out.text = node.text
}
if (node.children?.length) {
out.nest = []
for (const child of node.children) {
out.nest.push(traverse(child))
}
}
return out
}
}

// function define(
// lang: string,
Expand Down
9 changes: 9 additions & 0 deletions code/shared/type/cast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3449,6 +3449,15 @@ export type CompileSwift = {
}
}

export type CompileToAst = {
input: {
format: string
file: {
path: string
}
}
}

export type CompressMp4WithFfmpeg = {
input: {
format: string
Expand Down
16 changes: 16 additions & 0 deletions code/shared/type/source/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,3 +721,19 @@ export const compile_llvm_ir_to_assembly: Form = {
},
},
}

export const compile_to_ast: Form = {
form: 'form',
link: {
input: {
link: {
format: { like: 'string', name: { mark: 'I' } },
file: {
link: {
path: { like: 'string' },
},
},
},
},
},
}
11 changes: 11 additions & 0 deletions code/shared/type/take.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,17 @@ export const CompileSwiftModel: z.ZodType<Cast.CompileSwift> = z.object(
},
)

export const CompileToAstModel: z.ZodType<Cast.CompileToAst> = z.object(
{
input: z.object({
format: z.string(),
file: z.object({
path: z.string(),
}),
}),
},
)

export const CompressMp4WithFfmpegModel: z.ZodType<Cast.CompressMp4WithFfmpeg> =
z.object({
input: z.object({
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
"svgexport": "^0.4.2",
"tmp-promise": "^3.0.3",
"tree-sitter": "^0.20.6",
"tree-sitter-javascript": "^0.20.1",
"tree-sitter-swift": "^0.3.6",
"ts-custom-error": "^3.3.1",
"tsc-alias": "^1.8.8",
"type-detect": "^4.0.8",
Expand Down
28 changes: 28 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions test/file/code/quicksort/quicksort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
function quicksort(array, left, right) {
var index, pivot, l, r, temp

if (array.length > 1) {
left = typeof left === 'undefined' ? 0 : left
right = typeof right === 'undefined' ? array.length - 1 : right

// partition(array, left, right)
pivot = array[Math.floor((right + left) / 2)]
l = left
r = right

while (l <= r) {
while (array[l] < pivot) {
l++
}

while (array[r] > pivot) {
r--
}

if (l <= r) {
// swap(array, l, r) {
temp = array[l]
array[l] = array[r]
array[r] = temp
// end swap

l++
r--
}
}

index = l
// end partition

if (left < index - 1) {
quicksort(array, left, index - 1)
}

if (index < right) {
quicksort(array, index, right)
}
}

return array
}

0 comments on commit d3e179f

Please sign in to comment.