diff --git a/.vscode/extensions/disco.vsix b/.vscode/extensions/disco.vsix new file mode 100644 index 0000000..2fea99e Binary files /dev/null and b/.vscode/extensions/disco.vsix differ 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 02fc381..e0aa67f 100755 Binary files a/disco_test and b/disco_test differ 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.