From b4fd6a999b2b2b9197efd168f6ae908527a09d1e Mon Sep 17 00:00:00 2001 From: Max Wash Date: Mon, 27 Jan 2025 19:19:11 +0000 Subject: [PATCH] doc: add intermediate representation language --- doc/ir/Sum.ir | 105 +++++++++++++++++++++++++++++++++++++++++++ doc/sample/Simple.im | 16 +------ doc/sample/Sum.im | 23 +++++----- 3 files changed, 118 insertions(+), 26 deletions(-) create mode 100644 doc/ir/Sum.ir diff --git a/doc/ir/Sum.ir b/doc/ir/Sum.ir new file mode 100644 index 0000000..c296749 --- /dev/null +++ b/doc/ir/Sum.ir @@ -0,0 +1,105 @@ +record source_filename = "Sum.im" + +record package_scope = "net.doorstuck.test" + +data @.str.0 = str "Finds the sum of a set of numbers." +data @.str.1 = str "Number (blank to finish): " + +define iblock { + ; cout put:'Finds the sum of a set of numbers.' + %1 = alloca id + store id @.str.0, ptr %1 + + %2 = load ptr %1 + %3 = load id @cout + + call @_M03putE(id %3, id %2) + +L0001.cond: + br label @L0001.body + +L0001.body: + ; cout + %4 = load id @cout + + ; 'Number (blank to finish): ' + %5 = alloca id + store str @.str.1, ptr %5 + + ;; cout print:'Number (blank to finish): ' + call @_M05printE(id %4, id %5) + + + ; cout + %6 = load id @cout + + ;; cout flush + call @_M5flushE(id %6) + + + ; v + %7 = alloca i32 + + ; 0 + %8 = i32 #0 + + ;; v = 0 + store i32 %8, ptr %7 + + + ; input + %9 = alloca id + + ; cin + %10 = load id @cin + + ; cin readLine + %11 = call @_M8readLineE(id %10) + + ;; input = cin readLine + store id %11, ptr %9 + +L0002.if: + ; '' + %12 = load ptr @String + %13 = call @_M3newE(id %12) + + ; input + %14 = load ptr %9 + + ; input == '' + %15 = cmp eq id %14, %13 + + ;; if input == '' + br i1 %15, label @L0002.then, label @L0002.else + +L0002.then: + ;; then + + ; break + br label @L0001.end + + br label @L0002.end + +L0002.else: + ;; else + br label @L0002.end + +L0002.end: + ;; end + + ; Int + %17 = load id @Int + + ; input + %18 = load ptr %9 + + ; Int parse:input + %19 = call @_M5parseE(id %17, id %18) + + ;; v = Int parse:input + store id %19, ptr %7 + + +L0001.end: +} diff --git a/doc/sample/Simple.im b/doc/sample/Simple.im index 0ecefab..4c70e8f 100644 --- a/doc/sample/Simple.im +++ b/doc/sample/Simple.im @@ -1,15 +1,3 @@ -package simple.pkg +v = 2 + 3 * 4. -use std.io - -class Simple - - init(name:name age:age) - v = 1 + 2 squared + not 3. - self::name = name. - self::age = age! - - - name | ^self::name. - - - age - ^self::age! -end +cout put:v diff --git a/doc/sample/Sum.im b/doc/sample/Sum.im index f6557d7..88cdf4c 100755 --- a/doc/sample/Sum.im +++ b/doc/sample/Sum.im @@ -1,29 +1,28 @@ package net.doorstuck.test -cout put:'Finds the sum of a set of numbers.' +cout put:'Finds the sum of a set of numbers.'. -sum = 0 +sum = 0. while true do - cout print:'Number (blank to finish): ' - cout flush + cout print:'Number (blank to finish): '. + cout flush. - input = '' - v = 0 - cin get:input + v = 0. + input = cin readLine. if input == '' then break end - try - v = Int parse:input - catch (#err:number_format, err) - cout put:'{input} is not a valid number.' + v = Int parse:input. + + if v == null then + cout put:'{input} is not a valid number.'. continue end - sum += v + sum += v. end cout put:'Sum: {sum}'