Skip to content

Instantly share code, notes, and snippets.

@7shi
7shi / main.cpp
Last active December 14, 2015 04:09 — forked from bencz/main.cpp
#include <cstdio>
#include <vector>
#include <windows.h>
using namespace std;
class Buffer: public vector<unsigned char>
{
public:
void push_dword(DWORD dw)
@7shi
7shi / gist:4121619
Created November 20, 2012 22:15 — forked from bencz/gist:4115521
Basic compiler, generate PE EXE
/*
INPUT SAMPLE:
VAR NUMBER = 25
VAR STRING = "ALEX"
VAR ERRO = "THAT's NOT CAUSE AN ERROR :)"
PRINT ERRO
PRINT "ALEXANDRE"
PRINT NUMBER
def rpn(str: String): Int = rpnPrime(str.split(' ').toList, Nil)
def rpnPrime(str: List[String], stack: List[Int]): Int = (str, stack) match {
case ("+"::t, y::x::zs) => rpnPrime(t, (x + y) :: zs)
case ("-"::t, y::x::zs) => rpnPrime(t, (x - y) :: zs)
case ("*"::t, y::x::zs) => rpnPrime(t, (x * y) :: zs)
case ("/"::t, y::x::zs) => rpnPrime(t, (x / y) :: zs)
case ("%"::t, y::x::zs) => rpnPrime(t, (x % y) :: zs)
case ( n::t, zs) => rpnPrime(t, (n.toInt) :: zs)
case _ => stack.head
import scala.language.implicitConversions
// R is derived from Rational with some fixes.
// https://sites.google.com/site/scalajp/home/documentation/scala-by-example/chapter6
case class R(numer: Int, denom: Int) extends Ordered[R] {
def reduce: R = {
def gcd(x: Int, y: Int): Int = {
if (x == 0) y
else if (x < 0) gcd(-x, y)
import scala.language.implicitConversions
sealed trait Expr {
def +(that: Expr): Expr = Add(List(this, that))
def *(that: Expr): Expr = Mul(List(this, that))
def +(that: Int ): Expr = this + N( that)
def -(that: Int ): Expr = this + N(-that)
def *(that: Int ): Expr = this * N( that)
import scala.language.implicitConversions
// R is derived from Rational with some fixes.
// https://sites.google.com/site/scalajp/home/documentation/scala-by-example/chapter6
case class R(n: Int, d: Int) extends Ordered[R] {
private def gcd(x: Int, y: Int): Int = {
if (x == 0) y
else if (x < 0) gcd(-x, y)
else if (y < 0) -gcd( x, -y)
sealed trait Expr
case class N(n: Int) extends Expr
case class Var(x: String, a: Int, n: Int) extends Expr
case class Add(n: Expr*) extends Expr
case class Mul(n: Expr*) extends Expr
def x(a: Int, n: Int): Var = {
Var("x", a, n)
}
sealed trait Expr
case class N(n: Int) extends Expr
case class Var(x: String, a: Int, n: Int) extends Expr
case class Add(n: Expr*) extends Expr
case class Mul(n: Expr*) extends Expr
def add(xs: List[Expr]): Expr = xs match {
case List() => N(0)
case List(x) => x
case _ => Add(xs.toArray: _*) // Add(WrappedArray(N(1), N(2), Var(x,2,2)))
sealed trait Expr
case class N(n: Int) extends Expr
case class Add(n: Expr*) extends Expr
case class Mul(n: Expr*) extends Expr
def str(e: Expr): String = e match {
case N(x) => x.toString
case Add() => ""
case Add(x) => str(x)
case Add(x, xs@_*) => str(x) ++ "+" ++ str(Add(xs: _*))
@7shi
7shi / deep_cpy.cpp
Last active August 29, 2015 14:21 — forked from shigemk2/deep_cpy.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct A{
char s[10000];
A() {
strcpy(s, "123");
}