Git Product home page Git Product logo

pinn's Introduction

Pinn. It lets you be correct.

Implement Pinn entirely in golang (remove ANTLR) in x64/ARM64 assembly (eventually binaries). Heavily inspired by Golang, with a few tweaks to make it less brittle. The biggest tweak is not naming the language after a common verb. A ternary expression is allowed. Use it with care.

Pinn

Motivations

  • ANTLR works well, but a clean hand written implementation should be faster. Also could not be there for a self-hosting eventual solution.
  • LLVM was looked at. Go had an initial implementation in LLVM, but benefited from a complete self-hosted toolchain.
  • ARM64 is set to appear on Macs. Owns phone space. Windows 10 now runs on it. It's fairly clean (especially compared to x64). x64 is still dominant. 32-bit versions are not prevalent anywhere, and would complicated things--especially x32.

Steps

  • Lexer. text/scanner mostly works because of similarity to Go.
  • Parser. LL(1). Small hack to lower precedence of range : operator in ternary ? : expressions.
  • Generate x64/ARM64 assembly code. ARM64 is lapsed. Main platform is Mac/x64.
    • Output binaries for Mac/Linux eventually in ARM64 and x64. LLVM has been rejected along with assemblers. This aligns with Golang.

Tic-tac-toe solver as a unit test.

#define SIZE 3;
#define SQSIZE SIZE SIZE *;

//TIC TAC TOE as unit test
EMPTY := 0;
FALSE := 0;
TRUE := 1;
TIE := 3;
PLAYER_A := 1;
PLAYER_B := 2;
count := 0;

func minimax (player int, depth int, flob [SQSIZE]int) int
{
 var da, db, x, y int;
  var mxy int;
 count++;
 i := 0;
 result := 0;
 var best int;
da = dbg();
  best = opposite(player);
  result = winner(flob);
        db = dbg();
        assert(da, db);

  if result != TIE {
    return result;
  }
  if full(flob) == TRUE {
        return TIE;
  }
  x = 0;
  y = 0;
  for x = 0; x < SIZE; x++ {
    for y = 0; y < SIZE; y++ {

      if flob[xy(x, y)]  == EMPTY {
        flob[xy(x, y)] = player;
        result = minimax(opposite(player), depth + 1, flob);
        if result == player {
          return player;
        }
        if result == TIE {
          best = TIE;
        }
        flob[xy(x, y)] = EMPTY;
 
      }

    }
  }
  return best;
}

func printBoard (flob [SQSIZE]int) {
  println();
  var i, j int;
  for i = 0; i < SIZE; i++ {
    for j = 0; j < SIZE; j++ {
      print(flob[xy(i, j)]);
    }
    println();
  }
  println();
}

func xy (fx, fy int) int {
  rt := fy * SIZE;
  rt += fx;
  return rt;
}


func full (flob [SQSIZE]int) int {
  i := 0;
  for i = 0; i < SQSIZE; i++ {
    if flob[i] == EMPTY {
      return FALSE;
    }
  }
  return TRUE;
}

func line (x int, y int, dx int, dy int, flob [SQSIZE]int) int {
    comp := flob[xy(x, y)];
    if comp == EMPTY {
        return TIE;
    }
    loop {
      if x + dx < SIZE {
        if y + dy < SIZE {
          x += dx;
          y += dy;
          if flob[xy(x, y)] != comp {
            return TIE;
          }
          continue;
        }
      }
      break;
    }
    return comp;
}

func winner (flob [SQSIZE]int) int {
  var ii int;
  rt := 0;
  for ii = 0; ii < SIZE; ii++ {

        rt = line(ii, 0, 0, 1, flob);
        if rt != TIE
            return rt;
        rt = line(0, ii, 1, 0, flob);
        if rt != TIE
            return rt;
    }
    rt = line(0, 0, 1, 1, flob);
    if rt != TIE {
        return rt;
    }
    rt = line(0, SIZE - 1, 1, -1, flob);
    return rt;
}

func opposite (opx int) int { 
 if opx == PLAYER_A {
   return PLAYER_B;
 }
 return PLAYER_A;
 }


func main() {
  var flob [SQSIZE]int;
  x := minimax(PLAYER_A, 0, flob);
  assert(x, TIE);
  assert(count, 0x17302);
  
}
main();

pinn's People

Contributors

keppel2 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.