Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into mrbgems
Browse files Browse the repository at this point in the history
  • Loading branch information
bovi committed Sep 19, 2012
2 parents 17c8770 + 1afda93 commit c0ff4fe
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 105 deletions.
33 changes: 25 additions & 8 deletions doc/coding_conventions.md → CONTRIBUTING.md
@@ -1,14 +1,31 @@
# Coding conventions
# How to contribute

mruby is an open-source project which is looking forward to each contribution.

## Your Pull Request

To make it easy to review and understand your change please keep the following
things in mind before submitting your pull request:

* Work on the latest possible state of **mruby/master**
* Test your changes before creating a pull request (**make test**)
* If possible write a test case which confirms your change
* Don't mix several features or bug-fixes in one pull request
* Create a branch which is dedicated to your change
* Create a meaningful commit message
* Explain your change (i.e. with a link to the issue you are fixing)

## Coding conventions

How to style your C and Ruby code which you want to submit.

## C code
### C code

The core part (parser, bytecode-interpreter, core-lib, etc.) of mruby is
written in the C programming language. Please note the following hints for your
C code:

### Comply with C99 (ISO/IEC 9899:1999)
#### Comply with C99 (ISO/IEC 9899:1999)

mruby should be highly portable to other systems and compilers. For that it is
recommended to keep your code as close as possible to the C99 standard
Expand All @@ -17,32 +34,32 @@ recommended to keep your code as close as possible to the C99 standard
Although we target C99, VC is also an important target for mruby, so that we
avoid local variable declaration in the middle.

### Reduce library dependencies to a minimum
#### Reduce library dependencies to a minimum

The dependencies to libraries should be put to an absolute minimum. This
increases the portability but makes it also easier to cut away parts of mruby
on-demand.

### Don't use C++ style comments
#### Don't use C++ style comments

/* This is the prefered comment style */

Use C++ style comments only for temporary comment e.g. commenting out some code lines.

### Insert a break after the method return value:
#### Insert a break after the method return value:

int
main(void)
{
...
}

## Ruby code
### Ruby code

Parts of the standard library of mruby is written in the Ruby programming language
itself. Please note the following hints for your Ruby code:

### Comply with the Ruby standard (ISO/IEC 30170:2012)
#### Comply with the Ruby standard (ISO/IEC 30170:2012)

mruby is currently targeting to execute Ruby code which complies to ISO/IEC
30170:2012 (http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=59579).
37 changes: 37 additions & 0 deletions cmake/Toolchain-OSX-GenericShElf.cmake.sample
@@ -0,0 +1,37 @@
#
# Typical usage:
# 0) install cmake version 2.8-9 or higher.
# 1) install a PizzaFactory cross compiler
# a) darwin toolchain targeting sh-elf: http://sourceforge.jp/projects/pf3gnuchains/downloads/50061/sh-pizzafactory-elf.pkg/
# b) install pkg.
# c) export PATH=$PATH:/pizza/bin
# 2) cp cmake/Toolchain-OSX-GenericShElf.cmake.sample ~/Toolchain-OSX-GenericShElf.cmake
# 3) tweak toolchain values as needed
# 4) cd build
# 5) cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-OSX-GenericShElf.cmake ..
# 6) Run mirb on gdb
# a) sh-pizzafactory-elf-gdb tools/mirb/mirb
# b) target sim
# c) load
# d) run

# name of the target OS on which the built artifacts will run
# and the toolchain prefix
set(CMAKE_SYSTEM_NAME Generic)
set(TOOLCHAIN_PREFIX sh-pizzafactory-elf)

# cross compilers to use for C and C++
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)

# target environment(s) on the build host system
# set 1st to dir with the cross compiler's C/C++ headers/libs
# set 2nd to dir containing personal cross development headers/libs
set(CMAKE_FIND_ROOT_PATH /pizza/${TOOLCHAIN_PREFIX})

# modify default behavior of FIND_XXX() commands to
# search for headers/libs in the target environment and
# search for programs in the build host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
3 changes: 1 addition & 2 deletions include/mrbconf.h
Expand Up @@ -62,11 +62,10 @@ typedef double mrb_float;

#ifdef MRB_NAN_BOXING
typedef int32_t mrb_int;
typedef int32_t mrb_sym;
#else
typedef int mrb_int;
typedef intptr_t mrb_sym;
#endif
typedef short mrb_sym;

/* define ENABLE_XXXX from DISABLE_XXX */
#ifndef DISABLE_REGEXP
Expand Down
17 changes: 14 additions & 3 deletions mrblib/numeric.rb
@@ -1,6 +1,6 @@
##
# Integer
#
#
# ISO 15.2.8
class Integer

Expand All @@ -10,7 +10,6 @@ class Integer
#
# ISO 15.2.8.3.15
def downto(num, &block)
raise TypeError, "expected Integer" unless num.kind_of? Integer
i = self
while(i >= num)
block.call(i)
Expand Down Expand Up @@ -38,14 +37,26 @@ def times(&block)
#
# ISO 15.2.8.3.27
def upto(num, &block)
raise TypeError, "expected Integer" unless num.kind_of? Integer
i = self
while(i <= num)
block.call(i)
i += 1
end
self
end

##
# Calls the given block from +self+ to +num+
# incremented by +step+ (default 1).
#
def step(num, step=1, &block)
i = if num.kind_of? Float then self.to_f else self end
while(i <= num)
block.call(i)
i += step
end
self
end
end

##
Expand Down
57 changes: 29 additions & 28 deletions src/codegen.c
Expand Up @@ -466,7 +466,8 @@ node_len(node *tree)
return n;
}

#define lv_name(lv) ((mrb_sym)(lv)->car)
#define sym(x) ((mrb_sym)(intptr_t)(x))
#define lv_name(lv) sym((lv)->car)
static int
lv_idx(codegen_scope *s, mrb_sym id)
{
Expand Down Expand Up @@ -582,7 +583,7 @@ lambda_body(codegen_scope *s, node *tree, int blk)

dispatch(s, pos+i);
codegen(s, opt->car->cdr, VAL);
idx = lv_idx(s, (mrb_sym)opt->car->car);
idx = lv_idx(s, (mrb_sym)(intptr_t)opt->car->car);
pop();
genop_peep(s, MKOP_AB(OP_MOVE, idx, cursp()), NOVAL);
i++;
Expand Down Expand Up @@ -706,7 +707,7 @@ gen_values(codegen_scope *s, node *t)
static void
gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val)
{
mrb_sym sym = name ? name : (mrb_sym)tree->cdr->car;
mrb_sym sym = name ? name : sym(tree->cdr->car);
int idx;
int n = 0, noop = 0, sendv = 0, blk = 0;

Expand Down Expand Up @@ -796,11 +797,11 @@ gen_assignment(codegen_scope *s, node *node, int sp, int val)
node = node->cdr;
switch ((intptr_t)type) {
case NODE_GVAR:
idx = new_sym(s, (mrb_sym)node);
idx = new_sym(s, sym(node));
genop_peep(s, MKOP_ABx(OP_SETGLOBAL, sp, idx), val);
break;
case NODE_LVAR:
idx = lv_idx(s, (mrb_sym)node);
idx = lv_idx(s, sym(node));
if (idx > 0) {
if (idx != sp) {
genop_peep(s, MKOP_AB(OP_MOVE, idx, sp), val);
Expand All @@ -812,7 +813,7 @@ gen_assignment(codegen_scope *s, node *node, int sp, int val)
codegen_scope *up = s->prev;

while (up) {
idx = lv_idx(up, (mrb_sym)node);
idx = lv_idx(up, sym(node));
if (idx > 0) {
genop_peep(s, MKOP_ABC(OP_SETUPVAR, sp, idx, lv), val);
break;
Expand All @@ -824,19 +825,19 @@ gen_assignment(codegen_scope *s, node *node, int sp, int val)
}
break;
case NODE_IVAR:
idx = new_sym(s, (mrb_sym)node);
idx = new_sym(s, sym(node));
genop_peep(s, MKOP_ABx(OP_SETIV, sp, idx), val);
break;
case NODE_CVAR:
idx = new_sym(s, (mrb_sym)node);
idx = new_sym(s, sym(node));
genop_peep(s, MKOP_ABx(OP_SETCV, sp, idx), val);
break;
case NODE_CONST:
idx = new_sym(s, (mrb_sym)node);
idx = new_sym(s, sym(node));
genop_peep(s, MKOP_ABx(OP_SETCONST, sp, idx), val);
break;
case NODE_COLON2:
idx = new_sym(s, (mrb_sym)node->cdr);
idx = new_sym(s, sym(node->cdr));
genop_peep(s, MKOP_AB(OP_MOVE, cursp(), sp), NOVAL);
push();
codegen(s, node->car, VAL);
Expand All @@ -846,7 +847,7 @@ gen_assignment(codegen_scope *s, node *node, int sp, int val)

case NODE_CALL:
push();
gen_call(s, node, attrsym(s, (mrb_sym)node->cdr->car), sp, val);
gen_call(s, node, attrsym(s, sym(node->cdr->car)), sp, val);
val = NOVAL; /* push should have done in gen_call() */
break;

Expand Down Expand Up @@ -1247,7 +1248,7 @@ codegen(codegen_scope *s, node *tree, int val)

case NODE_COLON2:
{
int sym = new_sym(s, (mrb_sym)tree->cdr);
int sym = new_sym(s, sym(tree->cdr));

codegen(s, tree->car, VAL);
pop();
Expand All @@ -1258,7 +1259,7 @@ codegen(codegen_scope *s, node *tree, int val)

case NODE_COLON3:
{
int sym = new_sym(s, (mrb_sym)tree);
int sym = new_sym(s, sym(tree));

genop(s, MKOP_A(OP_OCLASS, cursp()));
genop(s, MKOP_ABx(OP_GETMCNST, cursp(), sym));
Expand Down Expand Up @@ -1378,7 +1379,7 @@ codegen(codegen_scope *s, node *tree, int val)

case NODE_OP_ASGN:
{
mrb_sym sym = (mrb_sym)tree->cdr->car;
mrb_sym sym = sym(tree->cdr->car);
int len;
const char *name = mrb_sym2name_len(s->mrb, sym, &len);
int idx;
Expand Down Expand Up @@ -1592,7 +1593,7 @@ codegen(codegen_scope *s, node *tree, int val)

case NODE_LVAR:
if (val) {
int idx = lv_idx(s, (mrb_sym)tree);
int idx = lv_idx(s, sym(tree));

if (idx > 0) {
genop(s, MKOP_AB(OP_MOVE, cursp(), idx));
Expand All @@ -1602,7 +1603,7 @@ codegen(codegen_scope *s, node *tree, int val)
codegen_scope *up = s->prev;

while (up) {
idx = lv_idx(up, (mrb_sym)tree);
idx = lv_idx(up, sym(tree));
if (idx > 0) {
genop(s, MKOP_ABC(OP_GETUPVAR, cursp(), idx, lv));
break;
Expand All @@ -1617,7 +1618,7 @@ codegen(codegen_scope *s, node *tree, int val)

case NODE_GVAR:
{
int sym = new_sym(s, (mrb_sym)tree);
int sym = new_sym(s, sym(tree));

genop(s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym));
push();
Expand All @@ -1626,7 +1627,7 @@ codegen(codegen_scope *s, node *tree, int val)

case NODE_IVAR:
{
int sym = new_sym(s, (mrb_sym)tree);
int sym = new_sym(s, sym(tree));

genop(s, MKOP_ABx(OP_GETIV, cursp(), sym));
push();
Expand All @@ -1635,7 +1636,7 @@ codegen(codegen_scope *s, node *tree, int val)

case NODE_CVAR:
{
int sym = new_sym(s, (mrb_sym)tree);
int sym = new_sym(s, sym(tree));

genop(s, MKOP_ABx(OP_GETCV, cursp(), sym));
push();
Expand All @@ -1644,7 +1645,7 @@ codegen(codegen_scope *s, node *tree, int val)

case NODE_CONST:
{
int sym = new_sym(s, (mrb_sym)tree);
int sym = new_sym(s, sym(tree));

genop(s, MKOP_ABx(OP_GETCONST, cursp(), sym));
push();
Expand Down Expand Up @@ -1828,7 +1829,7 @@ codegen(codegen_scope *s, node *tree, int val)

case NODE_SYM:
if (val) {
int sym = new_sym(s, (mrb_sym)tree);
int sym = new_sym(s, sym(tree));

genop(s, MKOP_ABx(OP_LOADSYM, cursp(), sym));
push();
Expand Down Expand Up @@ -1874,8 +1875,8 @@ codegen(codegen_scope *s, node *tree, int val)

case NODE_ALIAS:
{
int a = new_msym(s, (mrb_sym)tree->car);
int b = new_msym(s, (mrb_sym)tree->cdr);
int a = new_msym(s, sym(tree->car));
int b = new_msym(s, sym(tree->cdr));
int c = new_msym(s, mrb_intern(s->mrb, "alias_method"));

genop(s, MKOP_A(OP_TCLASS, cursp()));
Expand All @@ -1895,7 +1896,7 @@ codegen(codegen_scope *s, node *tree, int val)

case NODE_UNDEF:
{
int sym = new_msym(s, (mrb_sym)tree);
int sym = new_msym(s, sym(tree));
int undef = new_msym(s, mrb_intern(s->mrb, "undef_method"));

genop(s, MKOP_A(OP_TCLASS, cursp()));
Expand Down Expand Up @@ -1934,7 +1935,7 @@ codegen(codegen_scope *s, node *tree, int val)
push();
}
pop(); pop();
idx = new_msym(s, (mrb_sym)tree->car->cdr);
idx = new_msym(s, sym(tree->car->cdr));
genop(s, MKOP_AB(OP_CLASS, cursp(), idx));
idx = scope_body(s, tree->cdr->cdr->car);
genop(s, MKOP_ABx(OP_EXEC, cursp(), idx));
Expand All @@ -1960,7 +1961,7 @@ codegen(codegen_scope *s, node *tree, int val)
codegen(s, tree->car->car, VAL);
}
pop();
idx = new_msym(s, (mrb_sym)tree->car->cdr);
idx = new_msym(s, sym(tree->car->cdr));
genop(s, MKOP_AB(OP_MODULE, cursp(), idx));
idx = scope_body(s, tree->cdr->car);
genop(s, MKOP_ABx(OP_EXEC, cursp(), idx));
Expand All @@ -1987,7 +1988,7 @@ codegen(codegen_scope *s, node *tree, int val)

case NODE_DEF:
{
int sym = new_msym(s, (mrb_sym)tree->car);
int sym = new_msym(s, sym(tree->car));
int idx = lambda_body(s, tree->cdr, 0);

genop(s, MKOP_A(OP_TCLASS, cursp()));
Expand All @@ -2004,7 +2005,7 @@ codegen(codegen_scope *s, node *tree, int val)
case NODE_SDEF:
{
node *recv = tree->car;
int sym = new_msym(s, (mrb_sym)tree->cdr->car);
int sym = new_msym(s, sym(tree->cdr->car));
int idx = lambda_body(s, tree->cdr->cdr, 0);

codegen(s, recv, VAL);
Expand Down

0 comments on commit c0ff4fe

Please sign in to comment.