From 6f7871980941da3b6ac3cac5a430cc5ee9281dc9 Mon Sep 17 00:00:00 2001 From: Bronwen Date: Sun, 13 Mar 2022 23:15:17 -0400 Subject: [PATCH] mac support, and vscode language! --- .vscode/extensions/disco.vsix | Bin 0 -> 2825 bytes disco.disco | 14 +--- disco_test | Bin 9192 -> 33024 bytes disco_test.asm | 27 ++----- linkables/log.js | 31 +++++++- package.json | 3 +- src/compiler.ts | 20 +++++- src/disco.ts | 28 +++++++- .../.vscode/launch.json | 17 +++++ .../disco-language-support/.vscodeignore | 4 ++ .../disco-language-support/CHANGELOG.md | 9 +++ .../vscode/disco-language-support/README.md | 3 + .../language-configuration.json | 30 ++++++++ .../disco-language-support/package.json | 25 +++++++ .../syntaxes/disco.tmLanguage.json | 68 ++++++++++++++++++ .../vsc-extension-quickstart.md | 29 ++++++++ 16 files changed, 267 insertions(+), 41 deletions(-) create mode 100644 .vscode/extensions/disco.vsix create mode 100644 support/vscode/disco-language-support/.vscode/launch.json create mode 100644 support/vscode/disco-language-support/.vscodeignore create mode 100644 support/vscode/disco-language-support/CHANGELOG.md create mode 100644 support/vscode/disco-language-support/README.md create mode 100644 support/vscode/disco-language-support/language-configuration.json create mode 100644 support/vscode/disco-language-support/package.json create mode 100644 support/vscode/disco-language-support/syntaxes/disco.tmLanguage.json create mode 100644 support/vscode/disco-language-support/vsc-extension-quickstart.md diff --git a/.vscode/extensions/disco.vsix b/.vscode/extensions/disco.vsix new file mode 100644 index 0000000000000000000000000000000000000000..2fea99e344d833613129c2d280e35b5a7daf3718 GIT binary patch literal 2825 zcmaJ@c{r4N8y@?}k`RSLj3vxaC$uSBw!uthvL%Tj%gk7Y#u8E1#EeReCD}r@u~keb z#x6u6MYcp)M(0?^Qr}G9M|6GXJn!|p-rxJr{k+d|-_HXxVPh8t0DwS%RFMZFdP3j8 zjuilK=L7&m0000Ak3(U6T(KCipN}gZ?SOH0M)}~1kZ|k=Re@kyu>L)lD3csh3sjzf zstwG2nUt z)FbDJ(aH9WGP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K=qHZeB0%ikHbiBS>C%e5V2i(i`n%($2Y*v&?9Pbs+QWKM+Zl=Kufz literal 0 HcmV?d00001 diff --git a/disco.disco b/disco.disco index cd84236..a26ac76 100644 --- a/disco.disco +++ b/disco.disco @@ -1,14 +1,2 @@ - - link log -const test = "Hello" -const test2 ="Hello2" -const test3 = 'Hello' -const a="5" -log(test) -log("World") -log("Hello\n \"Wor(l)d\"\n\\o/") - - - - +log("Hello World") \ No newline at end of file diff --git a/disco_test b/disco_test index 02fc3817cb0f24e3eadad3e7076d158b11308934..e0aa67fa43db5b84d68c0ca1576f1e2baa55e23d 100755 GIT binary patch literal 33024 zcmeI*&rcIU6u|Lmp-7NWxEPHlqDG@80x`z)K(rXpMMaSxftcvn(p73oYoLt~5>ib( zloLm<-ux3#4BR6%nwuNfbT{FGTmV~uq1CwL5OT5p=Yv)Xm zM`T(FoWSoT#sFHMpJ{ zXw$r0i$7C))cbrcVQqikSYOq-xL;TKzUq7R*C< zAG~pGpntIZ8x-s6lH6-SA=;H|()pwEc=UJ+{%|N7qwCaazeG%OdCtsB=_jS%D={pg ztuY8)+ayu_eUkoFYm-%-66(=in49g+q?6sLg-l9vZ3iW^j;GrlZ)1zS-+zCF&p92a%}g;{^6wDzh0;Qxzg{AN$5Gf4`U9?V;ic}BK3?e z%;nvgu1j4~S32n}GAE?d>-Q9mX_o#}kgFFB0R#|0009ILKmY**5I_I{1Q0*~0R#|0 z009ILKmY**5I_I{1Q0*~0R#|0009ILKmY**5I_I{1Q0*~0R#|0009ILKmY**{*A!6 zQ~KbnnYh2RcuTkVI;GEdR%M@Ycul_|8ku;b{c^PU-Q=5{oegJa{nJ;c^nO+LLxvS|?*R?++p{=^ty~nyY`JD6`uVk!E$GWGvRrVye2fLkh-f=UT zT<1h?_Sa5nouwgw00IagfB*srAb6ER^sGi}{@cdC$g z?X;UnxwGcTOd_2%$MuB*4Q6vGC~9-*SW{HSXC&I>B?OD|egSdHlIEAMR;k{Urr(1sZ<0F)0%$@4u%tD zGE~O_ey=|zM=hXTib-=cPa7ALc0u#BmoaIVHE%XG%-A)}o81k=S7Z=_+mZKL)_gs} z*ER1&_%`yHL-g5^yal<)JpCj1ts{8w9t1nS8@7Tl+YEMLZEdwwDwH;%+*mIx6{>e& z<;H5MChA2f7nc@m)s;G2uWc49i?^0xqa-$}>y0Y79sj;`82FFJk=uIYf$MGufg6O) zZU}D6YlcmLZ18M1Y;GflU4?BEg{`L{WK|hX`Y&w``{|pJXFup(Mql;8jL(VC7@DS4 zg8mF%)E_sD(c4x;&hQ$4J=3Cktw;@}Ia`r$>E>wEH-pzSC1|q>;27fQ*_tExNE1VQ aqG06p?MC@=j1KExa8$|JQKbr^{XYTKYJdR% diff --git a/disco_test.asm b/disco_test.asm index 091b005..8e5b7c5 100644 --- a/disco_test.asm +++ b/disco_test.asm @@ -1,28 +1,15 @@ + global _main section .data - VVQDDBDZ db 72,101,108,108,111,0 - EWXBIBSR db 72,101,108,108,111,50,0 - PJQDTHUC db 72,101,108,108,111,0 - ECIATSPU db 53,0 - GTZCFAMK db 87,111,114,108,100,0 - YDHYSXWS db 72,101,108,108,111,10,32,34,87,111,114,40,108,41,100,34,10,92,111,47,0 + UMRRSQMF db 72,101,108,108,111,32,87,111,114,108,100,0 section .text - global _start -_start: +_main: push rbp mov rbp, rsp - push VVQDDBDZ - push EWXBIBSR - push PJQDTHUC - push ECIATSPU - mov rdi, [rbp - 8] - call _log - mov rdi, GTZCFAMK - call _log - mov rdi, YDHYSXWS + mov rdi, UMRRSQMF call _log mov rsp, rbp pop rbp - mov rax, 60 + mov rax, 0x02000001 mov rdi, 0 syscall _log: @@ -37,12 +24,12 @@ _log_loop: jmp _log_loop _log_loop_end: mov rdx, rbx - mov rax, 1 + mov rax, 0x02000004 mov rdi, 1 pop rsi syscall push 10 - mov rax, 1 + mov rax, 0x02000004 mov rdi, 1 mov rsi, rsp mov rdx, 1 diff --git a/linkables/log.js b/linkables/log.js index cc14c24..87240d2 100644 --- a/linkables/log.js +++ b/linkables/log.js @@ -1,4 +1,30 @@ -module.exports = { +module.exports = process.platform === 'darwin' ? { + asmName: '_log', + asm: `\ + push rdi + mov rbx, 0 +_log_loop: + mov cl, [rdi] + cmp cl, 0 + je _log_loop_end + inc rdi + inc rbx + jmp _log_loop +_log_loop_end: + mov rdx, rbx + mov rax, 0x02000004 + mov rdi, 1 + pop rsi + syscall + push 10 + mov rax, 0x02000004 + mov rdi, 1 + mov rsi, rsp + mov rdx, 1 + syscall + pop rdi + ret` +} : { asmName: '_log', asm: `\ push rdi @@ -24,4 +50,5 @@ _log_loop_end: syscall pop rdi ret` -} \ No newline at end of file +} + diff --git a/package.json b/package.json index 053b449..779f0eb 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "scripts": { "build": "nasm -f elf64 disco.asm -o disco.o && ld disco.o -o disco", "start": "./disco", - "dev": "yarn build && yarn start" + "dev": "yarn build && yarn start", + "code": "code . --extensionDevelopmentPath=$(pwd)/support/vscode/disco-language-support" }, "dependencies": { "@types/node": "^17.0.21", diff --git a/src/compiler.ts b/src/compiler.ts index b121f96..891dbb8 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -21,6 +21,13 @@ const statements = []; const localVariables = new Map(); const sections = { + preamble() { + if(process.platform === 'darwin') { + return ' global _main\n'; + } else { + return ''; + } + }, data() { const convert = ([name, xs]) => name + ' db ' + xs.join(',') return 'section .data\n ' @@ -29,10 +36,17 @@ const sections = { .join('\n ') }, text() { - return 'section .text\n global _start\n_start:\n push rbp\n mov rbp, rsp\n ' + if(process.platform === 'darwin') { + return 'section .text\n_main:\n push rbp\n mov rbp, rsp\n ' + statements.join('\n ') - + '\n mov rsp, rbp\n pop rbp\n mov rax, 60\n mov rdi, 0\n syscall\n' + + '\n mov rsp, rbp\n pop rbp\n mov rax, 0x02000001\n mov rdi, 0\n syscall\n' + [...linkedLibraries.values()].map(({asmName, asm}) => asmName + ':\n' + asm).join('\n') + } else { + return 'section .text\n global _start\n_start:\n push rbp\n mov rbp, rsp\n ' + + statements.join('\n ') + + '\n mov rsp, rbp\n pop rbp\n mov rax, 60\n mov rdi, 0\n syscall\n' + + [...linkedLibraries.values()].map(({asmName, asm}) => asmName + ':\n' + asm).join('\n') + } } } @@ -123,5 +137,5 @@ export function compile(tree) { for(const item of tree.value) { compileStatement(item); } - return sections.data() + '\n' + sections.text(); + return sections.preamble() + sections.data() + '\n' + sections.text(); } \ No newline at end of file diff --git a/src/disco.ts b/src/disco.ts index 13fc55f..62a30d7 100755 --- a/src/disco.ts +++ b/src/disco.ts @@ -41,11 +41,35 @@ try { console.log(); console.log('=== nasm ==='); - require('child_process').execSync('nasm -f elf64 disco_test.asm -o disco_test.o', { stdio: 'inherit' }); + nasm(); console.log('=== ld ==='); - require('child_process').execSync('ld disco_test.o -o disco_test', { stdio: 'inherit' }); + ld(); console.log('=== execute ==='); require('child_process').execSync('./disco_test', { stdio: 'inherit' }); } catch (e) { process.exit(1); } + +function nasm() { + if(process.platform === 'darwin') { + require('child_process').execSync('nasm -f macho64 disco_test.asm -o disco_test.o', { stdio: 'inherit' }); + } else { + require('child_process').execSync('nasm -f elf64 disco_test.asm -o disco_test.o', { stdio: 'inherit' }); + } +} + +function ld() { + if(process.platform === 'darwin') { + require('child_process').execSync([ + 'ld', 'disco_test.o', + '-o', 'disco_test', + '-macosx_version_min', '11.0', + '-L', '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib', + '-lSystem' + ].join(' '), { + stdio: 'inherit' + }); + } else { + require('child_process').execSync('ld disco_test.o -o disco_test', { stdio: 'inherit' }); + } +} \ No newline at end of file diff --git a/support/vscode/disco-language-support/.vscode/launch.json b/support/vscode/disco-language-support/.vscode/launch.json new file mode 100644 index 0000000..0e191b5 --- /dev/null +++ b/support/vscode/disco-language-support/.vscode/launch.json @@ -0,0 +1,17 @@ +// A launch configuration that launches the extension inside a new window +// Use IntelliSense to learn about possible attributes. +// Hover to view descriptions of existing attributes. +// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Extension", + "type": "extensionHost", + "request": "launch", + "args": [ + "--extensionDevelopmentPath=${workspaceFolder}" + ] + } + ] +} \ No newline at end of file diff --git a/support/vscode/disco-language-support/.vscodeignore b/support/vscode/disco-language-support/.vscodeignore new file mode 100644 index 0000000..f369b5e --- /dev/null +++ b/support/vscode/disco-language-support/.vscodeignore @@ -0,0 +1,4 @@ +.vscode/** +.vscode-test/** +.gitignore +vsc-extension-quickstart.md diff --git a/support/vscode/disco-language-support/CHANGELOG.md b/support/vscode/disco-language-support/CHANGELOG.md new file mode 100644 index 0000000..193d9c1 --- /dev/null +++ b/support/vscode/disco-language-support/CHANGELOG.md @@ -0,0 +1,9 @@ +# Change Log + +All notable changes to the "disco-language-support" extension will be documented in this file. + +Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. + +## [Unreleased] + +- Initial release \ No newline at end of file diff --git a/support/vscode/disco-language-support/README.md b/support/vscode/disco-language-support/README.md new file mode 100644 index 0000000..d2f068a --- /dev/null +++ b/support/vscode/disco-language-support/README.md @@ -0,0 +1,3 @@ +# Disco Language Supprt + +its literally just syntax highlighting. diff --git a/support/vscode/disco-language-support/language-configuration.json b/support/vscode/disco-language-support/language-configuration.json new file mode 100644 index 0000000..8f162a0 --- /dev/null +++ b/support/vscode/disco-language-support/language-configuration.json @@ -0,0 +1,30 @@ +{ + "comments": { + // symbol used for single line comment. Remove this entry if your language does not support line comments + "lineComment": "//", + // symbols used for start and end a block comment. Remove this entry if your language does not support block comments + "blockComment": [ "/*", "*/" ] + }, + // symbols used as brackets + "brackets": [ + ["{", "}"], + ["[", "]"], + ["(", ")"] + ], + // symbols that are auto closed when typing + "autoClosingPairs": [ + ["{", "}"], + ["[", "]"], + ["(", ")"], + ["\"", "\""], + ["'", "'"] + ], + // symbols that can be used to surround a selection + "surroundingPairs": [ + ["{", "}"], + ["[", "]"], + ["(", ")"], + ["\"", "\""], + ["'", "'"] + ] +} \ No newline at end of file diff --git a/support/vscode/disco-language-support/package.json b/support/vscode/disco-language-support/package.json new file mode 100644 index 0000000..7d98248 --- /dev/null +++ b/support/vscode/disco-language-support/package.json @@ -0,0 +1,25 @@ +{ + "name": "disco-language-support", + "displayName": "Disco Language Support", + "description": "", + "version": "0.0.1", + "engines": { + "vscode": "^1.65.0" + }, + "categories": [ + "Programming Languages" + ], + "contributes": { + "languages": [{ + "id": "disco", + "aliases": ["Disco", "disco"], + "extensions": [".disco"], + "configuration": "./language-configuration.json" + }], + "grammars": [{ + "language": "disco", + "scopeName": "source.disco", + "path": "./syntaxes/disco.tmLanguage.json" + }] + } +} \ No newline at end of file diff --git a/support/vscode/disco-language-support/syntaxes/disco.tmLanguage.json b/support/vscode/disco-language-support/syntaxes/disco.tmLanguage.json new file mode 100644 index 0000000..5e82902 --- /dev/null +++ b/support/vscode/disco-language-support/syntaxes/disco.tmLanguage.json @@ -0,0 +1,68 @@ +{ + "$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json", + "name": "Disco", + "patterns": [ + { "include": "#Keywords" }, + { "include": "#DoubleQuoteString" }, + { "include": "#SingleQuoteString" }, + { "include": "#Identifier" }, + { "include": "#SingleLineComment" } + ], + "repository": { + "Keywords": { + "patterns": [{ + "name": "keyword.control.disco", + "match": "\\b(link|const)\\b" + }] + }, + "Identifier": { + "patterns": [{ + "name": "variable.other.constant.disco", + "match": "[A-Za-z][A-Za-z0-9]*" + }] + }, + "SingleLineComment": { + "name": "comment.line.double-slash.disco", + "begin": "(^[ \\t]+)?(?=//)", + "end": "(?=$)", + "beginCaptures": { + "1": { + "name": "punctuation.whitespace.comment.leading.js" + } + }, + "patterns": [ + { + "name": "comment.line.double-slash.js", + "begin": "//", + "beginCaptures": { + "0": { + "name": "punctuation.definition.comment.js" + } + }, + "end": "(?=$)" + } + ] + }, + "DoubleQuoteString": { + "name": "string.quoted.double.disco", + "begin": "\"", + "end": "\"", + "patterns": [ + { "include": "#EscapeCharacter" } + ] + }, + "SingleQuoteString": { + "name": "string.quoted.single.disco", + "begin": "'", + "end": "'", + "patterns": [ + { "include": "#EscapeCharacter" } + ] + }, + "EscapeCharacter": { + "name": "constant.character.escape.disco", + "match": "\\\\." + } + }, + "scopeName": "source.disco" +} \ No newline at end of file diff --git a/support/vscode/disco-language-support/vsc-extension-quickstart.md b/support/vscode/disco-language-support/vsc-extension-quickstart.md new file mode 100644 index 0000000..e4fc9de --- /dev/null +++ b/support/vscode/disco-language-support/vsc-extension-quickstart.md @@ -0,0 +1,29 @@ +# Welcome to your VS Code Extension + +## What's in the folder + +* This folder contains all of the files necessary for your extension. +* `package.json` - this is the manifest file in which you declare your language support and define the location of the grammar file that has been copied into your extension. +* `syntaxes/disco.tmLanguage.json` - this is the Text mate grammar file that is used for tokenization. +* `language-configuration.json` - this is the language configuration, defining the tokens that are used for comments and brackets. + +## Get up and running straight away + +* Make sure the language configuration settings in `language-configuration.json` are accurate. +* Press `F5` to open a new window with your extension loaded. +* Create a new file with a file name suffix matching your language. +* Verify that syntax highlighting works and that the language configuration settings are working. + +## Make changes + +* You can relaunch the extension from the debug toolbar after making changes to the files listed above. +* You can also reload (`Ctrl+R` or `Cmd+R` on Mac) the VS Code window with your extension to load your changes. + +## Add more language features + +* To add features such as intellisense, hovers and validators check out the VS Code extenders documentation at https://code.visualstudio.com/docs + +## Install your extension + +* To start using your extension with Visual Studio Code copy it into the `/.vscode/extensions` folder and restart Code. +* To share your extension with the world, read on https://code.visualstudio.com/docs about publishing an extension.